Dictionaries and loops

Bruno Desthuilliers bruno.42.desthuilliers at websiteburo.invalid
Mon Sep 8 09:46:57 EDT 2008


Mike P a écrit :
> Hi All
> i have a CSV file that i'm reading in and each line has the look of
> the below
> 
> {None: ['User-ID', 'Count']}
> {None: ['576460847178667334', '1']}
> {None: ['576460847178632334', '8']}

This doesn't look like a CSV file at all... Is that what you actually 
have in the file, or what you get from the csv.reader ???

> i want to make a dictionary of items in the form
> {576460847178667334:1, 576460847178632334:8, ..... } for all rows in
> the datafile
> 
> my code so far is thus:
> 
> dict1={}
> j=1
> for row in reader1:
>   if j==1:
>     j+=1
>     continue #thus allowing me to skip the first row
>   if j>1:

Drop this, and call reader1.next() before entering the loop.

>     for element in row.values():
>       for item in element:
>         if int(item)%2==0:
>           dict1[int(item)] = int(item)+1

You're repeating the same operation (building an int from a string) 
three time, where one would be enough:

        for item in element:
          item = int(item)
          if item %2 == 0: # or : if not item % 2:
            dict1[item] = item + 1

But this code is not going to yield the expected result...

> # i know this is the problem line as it's not picking the second item
> up just finding the first and increasing it, but i can't figure out
> how to correct this?

Mmm... What about learning Python instead of trying any random code ? 
Programming by accident won't take you very far, and you can't expect 
this neswgroup to do your own work.


Ok, assuming your CSV file looks like this - and you never have 
duplicate values for the User-id column:

# source.csv
"User-ID", "Count"
576460847178667334, 1
576460847178632334, 8'

Here's a possible solution:

result = {}
src = open("source.csv", "rb")
try:
   reader = csv.reader(src)
   reader.next()
   for row in reader:
       user_id, count = int(row[0]), int(row[1])
       result[user_id] = count
finally:
   src.close()

or more tersely:

src = open("source.csv", "rb")
try:
   reader = csv.reader(src)
   reader.next()
   result = dict(map(int, row) for row in reader)
finally:
   src.close()




More information about the Python-list mailing list