转换夹具的csv

时间:2019-11-06 08:54:31

标签: django

我无法使用csv2json.py转换 CSV 格式的测试数据,因此我可以在应该具有pk,模型和字段格式的灯具中使用相同的数据。 / p>

[
    {
        "pk": 1, 
        "model": "wkw2.Lawyer", 
        "fields": {
            "school": "The George Washington University Law School", 
            "last": "Babbas", 
            "firm_url": "http://www.graychase.com/babbas", 
            "year_graduated": "2005", 
            "firm_name": "Gray & Chase", 
            "first": "Amr A"
        }
    }
]

1 个答案:

答案 0 :(得分:0)

这是一次帮助我将CSV转换为JSON的代码。让我知道您是否还需要其他东西:

import csv
import json

csvfile = open('file.csv', 'r')
jsonfile = open('file.json', 'w')

fieldnames = ("FirstName","LastName","IDNumber","Message")
reader = csv.DictReader( csvfile, fieldnames)
for row in reader:
    json.dump(row, jsonfile)
    jsonfile.write('\n')