Python 读取csv 文件 转成json

def transjson(csvpath):
    tableData = []
    with open(csvpath,'r',encoding='utf-8') as csvfile:
        reader = csv.DictReader(csvfile)
        for row in reader:
            # 读取的内容是字典格式的
            tableData.append(dict(row))

        print(json.dumps(tableData,sort_keys=True,indent=2,ensure_ascii=False))


if __name__ == '__main__':
    transjson('demo.csv')

  

原文地址:https://www.cnblogs.com/qq735675958/p/13539830.html