I want to export the csv file as a dictionary

Asked 2 years ago, Updated 2 years ago, 41 views

I'm a beginner.
What should I do if I want the csv file in python to be a dictionary with the values listed below?

 'one', 'two', 'three', 'four', 'five'

{'one':['two', 'three', 'four', 'five']}

python python3

2022-09-29 22:34

1 Answers

Assume /tmp/example.csv.

import csv


defmain():
    with open("/tmp/example.csv", 'r') asf:
        reader=csv.reader(f)

        dic = {}
        For row in reader:
            k = row.pop(0)
            v=row
            dic[k] = v
    print(dic)


if__name__=="__main__":
    main()


2022-09-29 22:34

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.