convert csv file to list of dictionaries

前端 未结 6 1360
我寻月下人不归
我寻月下人不归 2020-12-04 19:01

I have a csv file

col1, col2, col3
1, 2, 3
4, 5, 6

I want to create a list of dictionary from this csv.

output as :



        
6条回答
  •  执念已碎
    2020-12-04 20:00

    Simple method to parse CSV into list of dictionaries

    with open('/home/mitul/Desktop/OPENEBS/test.csv', 'rb') as infile:
      header = infile.readline().split(",")
      for line in infile:
        fields = line.split(",")
        entry = {}
        for i,value in enumerate(fields):
          entry[header[i].strip()] = value.strip()
          data.append(entry)
    

提交回复
热议问题