How do I convert a csv file with one column to a dictionary in Python?

gmanread

I need some help with a assignment for python. The task is to convert a .csv file to a dictionary, and do some changes. The problem is that the .csv file only got 1 column, but 3 rows.

The .csv file looks like this in excel

             A                         B

1.male Bob West
2.female Hannah South
3.male Bruce North

So everything is in column A.

My code looks so far like this:

import csv
reader = csv.reader(open("filename.csv"))
d={}
for row in reader:
    d[row[0]]=row[0:]
print(d)

And the output

{'\ufeffmale Bob West': ['\ufeffmale Bob West'], 'female Hannah South': 
['female Hannah South'], 'male Bruce North': ['male Bruce North']}

but I want

{1 : Bob West, 2 : Hannah South, 3 : Bruce North}

The male/female should be changed with ID, (1,2,3). And i don´t know how to figure out the 1 column thing.

Thanks in advance.

Sufiyan Ghori

You can use dict comprehension and enumerate the csv object,

import csv

reader = csv.reader(open("filename.csv"))

x = {num+1:name[0].split(" ",1)[-1].rstrip() for (num, name) in enumerate(reader)}
print(x)

# output,
{1: 'Bob West', 2: 'Hannah South', 3: 'Bruce North'}

Or you can do it without using csv module simply by reading the file,

with open("filename.csv", 'r') as t:
    next(t) # skip first line
    x = {num+1:name.split(" ",1)[-1].strip() for (num, name) in enumerate(t)}

print(x)

# output,
{1: 'Bob West', 2: 'Hannah South', 3: 'Bruce North'}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to convert a two column csv file to a dictionary in python

How do I convert csv file to rdd

How do I read a 2 column csv file and create a dictionary?

Convert dictionary of lists to a CSV file using Python

How do I convert a csv file (16bit (high) color) to image in Python?

Reupload, How do i convert a php file to python dictionary?

How do i import a csv file with one column as timeformat hh:mm:ss and plot it in x axis with pandas?

How to convert CSV file to python dictionary

How do i convert a dictionary with unequal list as value to csv

How do I convert a double indexed dictionary to an excel file using one key as row index and the other key as column index?

How can I convert a CSV file, which is nested by means of columns, to a nested dictionary with Python?

How do I write the whole csv file into a dictionary using for loops

How can I convert a text file to a dictionary to be used in Python?

How do I convert a list of lists to a python dictionary of the following format?

How do I convert a .csv file to .db file using python?

How do I import a csv file with one column defaulting?

Convert csv file into python dictionary

How do I convert a list to dictionary with specific format in python?

How do i unescape a csv file in python

How do I convert list into dictionary in Python?

How to convert a dictionary and write it into a CSV file?

How do I convert three lists into one dictionary?

How do I create a dictionary from a csv using python

If I want to split data in one part of a csv file in pandas by another column in the same csv file how do I do that?

Applying python dictionary to column of a CSV file

How am I to convert the .txt file into dictionary - Python

How do I convert this dictionary to a dataframe in python?

How do I compare values within a column in a csv file in python?

How do I update every row of one column of a CSV with Python?