Python Forum
[SOLVED] Filling multidict from CSV file?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[SOLVED] Filling multidict from CSV file?
#1
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.
Reply
#2
please provide a sample of input.csv
Reply
#3
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.
Reply
#4
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question [SOLVED] Correct way to convert file from cp-1252 to utf-8? Winfried 8 546 Feb-29-2024, 12:30 AM
Last Post: Winfried
  pandas : problem with conditional filling of a column Xigris 2 594 Jul-22-2023, 11:44 AM
Last Post: Xigris
  Loop through json file and reset values [SOLVED] AlphaInc 2 1,962 Apr-06-2023, 11:15 AM
Last Post: AlphaInc
  [Solved by deanhystad] Create a zip file using zipfile library DZ_Galaxy 2 1,105 Aug-17-2022, 04:57 PM
Last Post: DZ_Galaxy
  Numpy error while filling up matrix with Characters august 4 1,799 Apr-13-2022, 10:28 PM
Last Post: august
  Updating a config file [solved] ebolisa 8 2,532 Nov-04-2021, 10:20 AM
Last Post: Gribouillis
  |SOLVED] Glob JPGs, read EXIF, update file timestamp? Winfried 5 2,412 Oct-21-2021, 03:29 AM
Last Post: buran
  [SOLVED] Read text file from some point till EOF? Winfried 1 1,911 Oct-10-2021, 10:29 PM
Last Post: Winfried
  [SOLVED] Input parameter: Single file or glob? Winfried 0 1,539 Sep-10-2021, 11:54 AM
Last Post: Winfried
Thumbs Up [SOLVED] Find last occurence of pattern in text file? Winfried 4 4,305 Aug-13-2021, 08:21 PM
Last Post: Winfried

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020