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


Messages In This Thread
[SOLVED] Filling multidict from CSV file? - by Winfried - Oct-24-2020, 07:13 PM
RE: Filling multidict from CSV file? - by Larz60+ - Oct-24-2020, 07:33 PM
RE: Filling multidict from CSV file? - by Winfried - Oct-24-2020, 07:47 PM
RE: Filling multidict from CSV file? - by Winfried - Oct-24-2020, 08:26 PM

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