On Thu, 05 Nov 2009 20:02:53 -0000, Leland <lelandp...@gmail.com> wrote:

Hi,

I always use readline(), strip(), split() and so on to parse a string.
Is there some elegant way to parse the following string into a
dictionary {'50MHZ_CLK_SRC' : 'U122.2, R1395.1'}?

NET_NAME
'50MHZ_CLK_SRC'
 '@TEST_LIB.TEST(SCH_1):50MHZ_CLK_SRC':
 C_SIGNAL='@test_lib.test(sch_1):\50mhz_clk_src\';
NODE_NAME     U122 2
 '@TEST_LIB.TEST(SCH_1):page92_i...@inf_logic.cy2305(CHIPS)':
 'CLK2': CDS_PINID='CLK2';
NODE_NAME     R1395 1
 '@TEST_LIB.TEST(SCH_1):page92_i...@inf_resistors.resistor(CHIPS)':
 'A': CDS_PINID='A';

Here's an inelegant way:

**** CODE ****

results = {}
net_name_next = False
net_name = None
node_names = []
for line in sourcefile:
  line = line.strip()
  if line.startswith('NET_NAME'):
    if net_name is not None:
      results[net_name] = ", ".join(node_names)
    net_name = None
    node_names = []
    net_name_next = True
  elif net_name_next:
    net_name = line.strip("'")
    net_name_next = False
  elif line.startswith('NODE_NAME'):
    node_names.append("{1}.{2}".format(*line.split()))

# Last time through
if net_name is not None:
  results[net_name] = ", ".join(node_names)

**** END CODE ****

If you're prepared to allow the dictionary values to be lists rather than strings, we can squash that down:

**** CODE ****

results = {None: []}
net_name = None
for line in sourcefile:
  line = line.strip()
  if line.startswith('NET_NAME'):
    net_name = sourcefile.next().strip(" \t\n'")
    results[net_name] = []
  elif line.startswith('NODE_NAME'):
    results[net_name].append("{1}.{2}".format(*line.split()))
del results[None]

**** END CODE ****

If you can guarantee that you won't meet a NODE_NAME before you see a NET_NAME then you can lose the messing about with results[None]. Having spent today picking up the pieces from an assumption that's rotted after several years, I'm not feeling that brave. A slightly less messy version of that would be:


**** CODE ****

results = {}
entry = []
for line in sourcefile:
  line = line.strip()
  if line.startswith('NET_NAME'):
    entry = []
    results[sourcefile.next().strip(" \t\n'")] = entry
  elif line.startswith('NODE_NAME'):
    entry.append("{1}.{2}".format(*line.split()))

**** END CODE ****

I'm a little dubious about doing mutable magic like this without copious comments, though.

--
Rhodri James *-* Wildebeest Herder to the Masses
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to