django - 为灯具转换 csv
问题描述
我无法使用CSV格式转换测试数据,csv2json.py
以便我可以在应该具有 pk、模型和字段格式的夹具中使用相同的数据。
[
{
"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"
}
}
]
解决方案
这是曾经帮助我将 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')
推荐阅读
- python - 如何使用 BeautifulSoup 从内部 div 标签中提取文本
- amazon-web-services - 如何绕过 AWS Lambda 大小限制
- java - 从 jdbc 调用 sqlj.install_jar?
- flutter - 复杂的可扩展列表视图颤动
- sql - SQL 查询的潜在性能问题
- python - 如何制作输入列表?
- python - 如何在 Python 中使用包含冒号(:) 和空格的数据框将对象转换为整数?
- firebase - 颤动如何固定列表中的条目
- python - 为什么 3 (int) 和 3.0 (float) 在 python 中是一样的?
- python - 多处理错误