Python Forum
creating dict out of CSV file without the headers - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: creating dict out of CSV file without the headers (/thread-33596.html)



creating dict out of CSV file without the headers - ranbarr - May-09-2021

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!


RE: creating dict out of CSV file without the headers - Yoriz - May-09-2021

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]



RE: creating dict out of CSV file without the headers - ranbarr - May-09-2021

(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


RE: creating dict out of CSV file without the headers - Yoriz - May-09-2021

What values?.
what does your csv file look like and how does your output differ from your expected output?


RE: creating dict out of CSV file without the headers - ranbarr - May-09-2021

(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


RE: creating dict out of CSV file without the headers - Yoriz - May-09-2021

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'}



RE: creating dict out of CSV file without the headers - ranbarr - May-09-2021

sorry didnt noticed, Thank you very much!