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
#1
Hi all,
Im pretty new to python and Im learing how to import CSV and make it a dictionary
I made it but I dont know how to remove the headers and the values are in a list and I need them not to lol
this is the code:
noc_dict = {}
def read_file_noc(file):
    with open("noc_countries.csv", "r") as my_csv:
        for line in my_csv:
            line = line.replace('\n', '')
            line = line.split(',')
            noc_dict[line[0]] = line[1:] 
   
return noc_dict
this is my output:
Output:
{'NOC': ['region'], 'AFG': ['Afghanistan'], 'ALB': ['Albania'], 'ALG': ['Algeria'], 'ASA': ['American Samoa'], 'AND': ['Andorra'], 'ANG': ['Angola'],
While it should be:
Output:
{'AFG': 'Afghanistan', 'ALB': 'Albania', 'ALG': 'Algeria', 'ASA': 'American Samoa', 'AND': 'Andorra', 'ANG': 'Angola',
I tried to find it online and solve it myself but I couldnt find the answer.. appreciate any help!
Reply
#2
Try using next to skip the first line of the file and index 1 to not have the values in a list.
noc_dict = {}
def read_file_noc():
    with open("noc_countries.csv", "r") as my_csv:
        next(my_csv)
        for line in my_csv:
            line = line.replace('\n', '')
            line = line.split(',')
            noc_dict[line[0]] = line[1]
ranbarr likes this post
Reply
#3
(May-09-2021, 07:44 PM)Yoriz Wrote: Try using next to skip the first line of the file and index 1 to not have the values in a list.
noc_dict = {}
def read_file_noc():
    with open("noc_countries.csv", "r") as my_csv:
        next(my_csv)
        for line in my_csv:
            line = line.replace('\n', '')
            line = line.split(',')
            noc_dict[line[0]] = line[1]
that worked! btw any idea how to remove the values from the lists? I didnt even tried to put them in list but they are
Reply
#4
What values?.
what does your csv file look like and how does your output differ from your expected output?
ranbarr likes this post
Reply
#5
(May-09-2021, 07:57 PM)Yoriz Wrote: What values?.
what does your csv file look like and how does your output differ from your expected output?


[Image: mFJknwC]
Added an Image of the CSV
the output I get is:
Output:
{'AFG': ['Afghanistan'], 'ALB': ['Albania'], 'ALG': ['Algeria'], 'ASA': ['American Samoa'], 'AND': ['Andorra'], 'ANG': ['Angola'],
which the values of the keys are in a list []

while the output it should be:
Output:
{'AFG': 'Afghanistan', 'ALB': 'Albania', 'ALG': 'Algeria', 'ASA': 'American Samoa', 'AND': 'Andorra', 'ANG': 'Angola',
which the key values are not in a list
Reply
#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
#7
sorry didnt noticed, Thank you very much!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Creating/Moving file DeadlyKnight 3 1,524 Mar-25-2022, 06:25 PM
Last Post: ibreeden
  Creating Disassembler for a bin file(total beginner) SoulsKeeper 1 2,463 Sep-04-2018, 04:15 PM
Last Post: Larz60+
  Creating a file with variable name but distinct extension Moeniac 1 2,269 Nov-27-2017, 05:47 PM
Last Post: DeaD_EyE
  Pandas: Accessing column headers zsad512 0 2,472 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