jeudi 15 mai 2014

python - convertir un fichier csv dans un dictionnaire avec plusieurs valeurs ? -Débordement de pile


I have a csv file like this:


pos,place
6696,266835
6698,266835
938,176299
940,176299
941,176299
947,176299
948,176299
949,176299
950,176299
951,176299
770,272944
2751,190650
2752,190650
2753,190650

I want to convert it to a dictionary like the following:


{266835:[6696,6698],176299:[938,940,941,947,948,949,950,951],190650:[2751,2752,2753]}

And then, fill the missing numbers in the range in the values:


{{266835:[6696,6697,6698],176299:[938,939,940,941,942,943,944,945,946947,948,949,950,951],190650:[2751,2752,2753]}
}

Right now i have tried to build the dictionary using solution suggested here, but it overwrites the old value with new one.


Any help would be great.


Here is a function that i wrote for converting csv2dict


def csv2dict(filename):
"""
reads in a two column csv file, and the converts it into dictionary
"""
import csv
with open(filename) as f:
f.readline()#ignore first line
reader=csv.reader(f,delimiter=',')
mydict=dict((rows[1],rows[0]) for rows in reader)
return mydict



Easiest is to use collections.defaultdict() with a list:


import csv
from collections import defaultdict

data = defaultdict(list)

with open(inputfilename, 'rb') as infh:
reader = csv.reader(infh)
next(reader, None) # skip the header

for col1, col2 in reader:
data[col2].append(int(col1))
if len(data[col2]) > 1:
data[col2] = range(min(data[col2]), max(data[col2]) + 1)

This also expands the ranges on the fly as you read the data.




Based on what you have tried -


from collections import default dict

# open archive reader
myFile = open ("myfile.csv","rb")
archive = csv.reader(myFile, delimiter=',')
arch_dict = defaultdict(list)

for rows in archive:
arch_dict[row[1]].append(row[0])

print arch_dict


I have a csv file like this:


pos,place
6696,266835
6698,266835
938,176299
940,176299
941,176299
947,176299
948,176299
949,176299
950,176299
951,176299
770,272944
2751,190650
2752,190650
2753,190650

I want to convert it to a dictionary like the following:


{266835:[6696,6698],176299:[938,940,941,947,948,949,950,951],190650:[2751,2752,2753]}

And then, fill the missing numbers in the range in the values:


{{266835:[6696,6697,6698],176299:[938,939,940,941,942,943,944,945,946947,948,949,950,951],190650:[2751,2752,2753]}
}

Right now i have tried to build the dictionary using solution suggested here, but it overwrites the old value with new one.


Any help would be great.


Here is a function that i wrote for converting csv2dict


def csv2dict(filename):
"""
reads in a two column csv file, and the converts it into dictionary
"""
import csv
with open(filename) as f:
f.readline()#ignore first line
reader=csv.reader(f,delimiter=',')
mydict=dict((rows[1],rows[0]) for rows in reader)
return mydict


Easiest is to use collections.defaultdict() with a list:


import csv
from collections import defaultdict

data = defaultdict(list)

with open(inputfilename, 'rb') as infh:
reader = csv.reader(infh)
next(reader, None) # skip the header

for col1, col2 in reader:
data[col2].append(int(col1))
if len(data[col2]) > 1:
data[col2] = range(min(data[col2]), max(data[col2]) + 1)

This also expands the ranges on the fly as you read the data.



Based on what you have tried -


from collections import default dict

# open archive reader
myFile = open ("myfile.csv","rb")
archive = csv.reader(myFile, delimiter=',')
arch_dict = defaultdict(list)

for rows in archive:
arch_dict[row[1]].append(row[0])

print arch_dict

0 commentaires:

Enregistrer un commentaire