Parsing Data, Storing into an array, Infinite Backslashes
Steven D'Aprano
steve at REMOVETHIScyber.com.au
Mon Jul 11 22:54:32 EDT 2005
On Mon, 11 Jul 2005 13:47:22 -0700, supercomputer at gmail.com wrote:
> I am using this function to parse data I have stored in an array.
>
> This is what the array looks like:
>
> [['Memory', '0', 'Summary', '0'], ['Memory', '0', 'Speed',
> 'PC3200U-30330'], ['Memory', '0', 'Type', 'DDR SDRAM'], ... ]
[snip more of the array]
> This is the code to parse the array:
>
> count=0
> place=0
place is not used in your function. Remove it.
> query=[]
> while 1:
> try:
> i=fetch.next()
What is fetch and what does fetch.next() do?
It is considered bad programming practice to use a variable i for anything
except for i in range(). i for "index", not i for "next record".
> except StopIteration:
> break
> if i[1] != count:
What is i? A list? A tuple? A dict? What is stored in it?
> ++count
> query.append(count)
Why are you appending the numeric value of count to the list query? Since
count starts at zero, and increases by one, your list is just [1, 2, 3, ...]
> qval=`query[count]`
It looks like you are setting the variable qval to the string
representation of a number. Backticks are being depreciated, you should
write this as qval = str(query[count]).
But if I have understood your program logic correctly, query[count] up
to this point is just count. So a much simpler way is to just use qval =
str(count).
> query[count]=qval+i[2]+"="+i[3]+", "
Impossible to know what this does since we don't know what i is. Hint: it
is easier to read and parse expressions by adding a small amount of
whitespace:
query[count] = qval + i[2] + "=" + i[3] + ", "
> print qval,"\n"
>
> When it runs I get an output similar to this.
>
> \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'Type=DDR
> SDRAM,
> \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'Size=512,
> \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'Slot=DIMM2/J13,
> \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'ConfigurationType=2,
> \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'Summary=0,
> \\\\\\\\\\\\\\\'Speed=PC3200U-30330, \\\\\\\'Type=DDR SDRAM,
> \\\'Size=512, \'Slot=DIMM3/J14, '
>
> When it's supposed to print just the plain text with the numbers etc.
See below for some further hints.
> I have changed these lines:
>
> qval=`query[count]`
> query[count]=qval+i[2]+"="+i[3]+", "
>
> To this:
>
> query[count]=query[count]+i[2]+"="+i[3]+", "
>
> I get this error:
>
> Traceback (most recent call last): File "infnode.py", line 60, in ?
> query[count]=query[count]+i[2]+"="+i[3]+", "TypeError: unsupported
> operand type(s) for +: 'int' and 'str'
Yes. query[count] is an integer equal to count. i[2] is who-knows-what.
"=" is a string. You can't add strings to ints.
> So I try and fix it by doing this:
>
> query[count]=`query[count]`+i[2]+"="+i[3]+", "
That is functionally equivalent to your first version.
> Can someone please point me in the right direction I am sure that the
> `query[count]` is causing the backslashes.
I doubt it very much. But you can test it by adding some print lines in
your code: change this:
qval=`query[count]`
query[count]=qval+i[2]+"="+i[3]+", "
to this:
print "Count is: ", count
print "query[count] is: ", query[count]
qval=`query[count]`
print "qval is: ", qval
query[count]=qval+i[2]+"="+i[3]+", "
print "query[count] changed.\nNew value is: ", query[count]
--
Steven.
More information about the Python-list
mailing list