Parsing Data, Storing into an array, Infinite Backslashes

Jeff Epler jepler at unpythonic.net
Mon Jul 11 17:22:55 EDT 2005


Your code is needlessly complicated.

Instead of this business
    while 1:
        try:
            i = fetch.next()
        except stopIteration:
            break
simply write:
    for i in fetch:
(if there's an explicit 'fetch = iter(somethingelse)' in code you did
not show, then get rid of that and just loop 'for i in somethingelse')

i[1] will never compare equal to count, because i[1] is always a string
and count is always an integer.  Integers and strings are never equal to
each other.

Wring code like
    x = "a string " + 3
does not work in Python.  You can either convert to a string and then
use the + operator to concatenate:
    x = "a string " + str(3)
or you can use %-formatting:
    x = "a string %s" % 3
("%s" accepts any sort of object, not just strings)

Using repr(...) (`...` is just a shorthand for this) is what is really
introducing the backslashes.  When it outputs a string, it quotes the
string using backslashes.  But you pass the old part of the prepared
string through it each time, which leads to doubling backslashes.

Below is a program I wrote to process the data in your message.  It prints
out
    Memory 2 Summary=0, Speed=PC3200U-30330, Type=DDR SDRAM, Size=512, Slot=DIMM2/J13, ConfigurationType=2
    Memory 3 Summary=0, Speed=PC3200U-30330, Type=DDR SDRAM, Size=512, Slot=DIMM3/J14, ConfigurationType=2
    Memory 0 Summary=0, Speed=PC3200U-30330, Type=DDR SDRAM, Size=512, Slot=DIMM0/J11, ConfigurationType=2
    Memory 1 Summary=0, Speed=PC3200U-30330, Type=DDR SDRAM, Size=512, Slot=DIMM1/J12, ConfigurationType=2
the result is out of order because the result of calling .items() on a
dict is in an arbitrary order.

Jeff

s = [['Memory', '0', 'Summary', '0'], ['Memory', '0', 'Speed',
 'PC3200U-30330'], ['Memory', '0', 'Type', 'DDR SDRAM'], ['Memory', '0',
 'Size', '512'], ['Memory', '0', 'Slot', 'DIMM0/J11'], ['Memory', '0',
 'ConfigurationType', '2'], ['Memory', '1', 'Summary', '0'], ['Memory',
 '1', 'Speed', 'PC3200U-30330'], ['Memory', '1', 'Type', 'DDR SDRAM'],
 ['Memory', '1', 'Size', '512'], ['Memory', '1', 'Slot', 'DIMM1/J12'],
 ['Memory', '1', 'ConfigurationType', '2'], ['Memory', '2', 'Summary',
 '0'], ['Memory', '2', 'Speed', 'PC3200U-30330'], ['Memory', '2',
 'Type', 'DDR SDRAM'], ['Memory', '2', 'Size', '512'], ['Memory', '2',
 'Slot', 'DIMM2/J13'], ['Memory', '2', 'ConfigurationType', '2'],
 ['Memory', '3', 'Summary', '0'], ['Memory', '3', 'Speed',
 'PC3200U-30330'], ['Memory', '3', 'Type', 'DDR SDRAM'], ['Memory', '3',
 'Size', '512'], ['Memory', '3', 'Slot', 'DIMM3/J14'], ['Memory', '3',
 'ConfigurationType', '2']]

query = {}

for a, b, c, d in s:
    if not query.has_key((a,b)): query[(a,b)] = []
    query[(a,b)].append("%s=%s" % (c, d))

for (a,b), v in query.items():
    print a, b, ", ".join(v)
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 196 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20050711/107d2662/attachment.sig>


More information about the Python-list mailing list