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


Messages In This Thread
creating dict out of CSV file without the headers - by ranbarr - May-09-2021, 07:28 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