CSV to Dictionary
Convert the content of a CSV file into a Python Dictionary
Python: 3.0    Anurag Rana    06 Sep 2021   0
Login to upvote
1
2
3
4
5
6
7
# Assuming there is a file a.csv in current folder with below content
# name,year
# Rana,1989
# Python,1995
python3 -c "import csv,json;print(json.dumps(dict(csv.reader(open('a.csv')))))"
# output
# {"name": "year", "Rana": "1989", "Python": "1995"}
Copy | Download as: .py file   PNG
Share on: FB   Twitter   LinkedIn   WA
©2021 - code108labs   Contributors   Hosted on DigitalOcean   Partner website: pythoncircle.com