How to convert csv to json in python?

前端 未结 5 908
走了就别回头了
走了就别回头了 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:16

    For those who like one-liners:

    import csv
    import json
    
    json_data = [json.dumps(d) for d in csv.DictReader(open('file.csv'))]
    

    Checkout this fiddle for a working example: https://pyfiddle.io/fiddle/5992b8f4-552f-4970-91b6-a52cdee16ebc/?i=true

提交回复
热议问题