How to convert csv to json in python?

前端 未结 5 903
走了就别回头了
走了就别回头了 2020-12-15 22:47

I\'m very new to programming, have been learning python from past 3/4 weeks and this is one of the assignments given.

Input



        
5条回答
  •  死守一世寂寞
    2020-12-15 23:24

    You can attempt it using this code :

    def inputfunction(lists):
     tmpdict = {}
     for element_index in range(len(lists)):
         tmpdict[headers[elementindex]] = lists[element_index]
     return tmpdict
    
    def run(filename):
     filelist = [eachline.split(',') for eachline in open(inputfile,'r')]
     headers = filelist[0]
     values = filelist[1:]
     finallist = []
     for lists in values:
         finallist.append(inputfunction(lists))
     return finallist
    

提交回复
热议问题