Python Forum
creating dict out of CSV file without the headers
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
creating dict out of CSV file without the headers
#6
Cannot see the csv but please don't use pictures in the future just cut and pasted a sample of the csv.
As I said before you need to change your indexing to 1
you have
noc_dict = {}
line = 'AFG, Afghanistan'

line = line.replace('\n', '')
line = line.split(',')
noc_dict[line[0]] = line[1:]
print(noc_dict)
which will output
Output:
{'AFG': [' Afghanistan']}
because [1:] is a list of the 1 index and all the rest of the items if there are any

you just want to use 1 as the index to get the item on its own
noc_dict = {}
line = 'AFG, Afghanistan'

line = line.replace('\n', '')
line = line.split(',')
noc_dict[line[0]] = line[1]
print(noc_dict)
Output:
{'AFG': ' Afghanistan'}
ranbarr likes this post
Reply


Messages In This Thread
RE: creating dict out of CSV file without the headers - by Yoriz - May-09-2021, 08:21 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Creating/Moving file DeadlyKnight 3 1,611 Mar-25-2022, 06:25 PM
Last Post: ibreeden
  Creating Disassembler for a bin file(total beginner) SoulsKeeper 1 2,564 Sep-04-2018, 04:15 PM
Last Post: Larz60+
  Creating a file with variable name but distinct extension Moeniac 1 2,345 Nov-27-2017, 05:47 PM
Last Post: DeaD_EyE
  Pandas: Accessing column headers zsad512 0 2,519 Jul-12-2017, 12:20 AM
Last Post: zsad512

Forum Jump:

User Panel Messages

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