module for working with the result set

john fabiani jfabiani at yolo.com
Mon May 17 21:02:52 EDT 2004


Diez B. Roggisch wrote:
> john fabiani wrote:
> 
> 
>>See this does not make sense to me.
>>
>>
>>mydata[0]
>>['5992      ', 'A', '5346      ', '                              ', 'XX
>>        ', 'Kathy Stromme                 ', 'RICHMOND AMERICAN HOMES
>>               ', '7250 WEST PEAK, ST # 212                ', '
> 
> 
> <snip>
> 
> well - its a list, and I bet its a list of columns. The columns are mostly
> strings (at least at the beginning) and obviously are space padded to match
> up to their size.
> 
> The code you posted earlier assumes that the result row is returned as dict,
> thus iterating over the row yields in the keys. Consider this example:
> 
> d = {"key1" : 10, "key2" : 20 }
> for key in d:
>     print key, d[key]
> 
> that results in
> 
> key1
> 10
> key2
> 20
> 
> notice how putting a dict as sequence in a for ... in ... : statement
> iterates the keys.
> 
> Now you don't get a dict, but a list of row-values. So your code
> 
> for field in mydata[0]:
>    print mydata[0][field]
> 
> iterates over the data you presented, yielding the  '5992      '
> as first value. Thats all. Of course using that value as index makes no
> sense.
> 
> To summarize: you expect data to be returned as dict per row, where the data
> is returned as list.
> 
> Use the cursors .description property to get a mapping  between name and
> index.
> 
> 
> 
OK thats great!  I did the following and it works.
for field in range(0,len(mydata[0])):
   print mydata[0][field]
following your thoughts on the keys and my mistake - can you tell me how 
to get into a dict format?

John



More information about the Python-list mailing list