Dictionaries and loops

Bruno Desthuilliers bruno.42.desthuilliers at websiteburo.invalid
Mon Sep 8 17:57:57 CEST 2008


Mike P a écrit :
> Thanks for the solution above,
> 
> The raw data looked like
> User-ID,COUNTS
> 576460840144207854,6
> 576460821700280307,2
> 576460783848259584,1
> 576460809027715074,3
> 576460825909089607,1
> 576460817407934470,1
> 
> and i used
> 
> CSV_INPUT1 = "C:/Example work/Attr_model/Activity_test.csv"
> fin1 = open(CSV_INPUT1, "rb")
> reader1 = csv.DictReader((fin1), [], delimiter=",")

This should have been:
   reader1 = csv.DictReader(fin1, delimiter=",")

or even just csv.DictReader(fin1), since IIRC ',' is the default 
delimiter (I'll let you check this by yourself...).

with which you would have:
[
   {'User-ID':'576460840144207854', 'count':'6'},
   {'User-ID':'576460821700280307', 'count':'2'},
   # etc...
]
> with the following outcome.
> {None: ['User-ID', 'COUNTS']}
> {None: ['576460840144207854', '6']}
> {None: ['576460821700280307', '2']}
> {None: ['576460783848259584', '1']}
> {None: ['576460809027715074', '3']}
> {None: ['576460825909089607', '1']}

And you didn't noticed anything strange ???

> So i can see csv.reader is what i should have been using

With only 2 values, DictReader is probably a bit overkill, yes.

> Thanks for the help

You're welcome. But do yourself a favour: take time to *learn* Python - 
at least the very basic (no pun) stuff like iterating over a sequence.



More information about the Python-list mailing list