Python Forum

Full Version: [SOLVED] Filling multidict from CSV file?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,

For some reason, the list that is filled from the CSV file is OK, but when printing the contents of the multidict, I get more than three values:

from collections import defaultdict

mydict = defaultdict(list)
with open('input.csv", mode='r') as infile:
	reader = list(csv.reader(infile))

	#OK!
	for row in reader:
		#print(row[2],row[5],row[6],row[10])

		#Use ZIP as key, and add multiple values to each key
		mydict[row[2]].append(row[5])
		mydict[row[2]].append(row[6])
		mydict[row[2]].append(row[10])

	#NOK! More than three values!
	for k in mydict:
		print(k,mydict[k])
Can you spot what I'm doing wrong?

Thank you.
please provide a sample of input.csv
It's a comma-separated file, eg.

zip,lat,lon,name
12345,46.15,4.92,Some place
FWIW, it opens fine in LibreOffice Calc.
Never mind, pandas worked right off the bat:

import pandas as pd
subset = pd.read_csv(CSVFILE)[["zip", "latitude", "longitude", "name"]]
print(subset.head(8))
Thank you.