python - 将编辑后的 CSV 输入写入 JSON 文件
问题描述
我正在读取一个 CSV 文件并输出一个 JSON 文件,然后由另一个应用程序处理该文件。下游应用程序所需的某些信息不存在于 CSV 文件中,需要以编程方式从其他来源获取。在输出 JSON 数据之前,有没有办法在 CSV 数据中编辑/插入字段?我一直在研究Python CSV 模块文档并注意到了这个for row in reader:
例子。我可以遍历已知字段,然后插入新字段以构建new_row
变量。我可以转储new_row
到 JSON 文件,但给原始文件的字段名称reader
不会随之而来,所以我最终得到了一个非常疯狂的new_row
变量,感觉不像我在保存任何东西。想法?
#!/usr/bin/python
import urllib
import json
import sys
import csv
fieldnames = ("first_name", "last_name","phone")
reader = csv.DictReader(csvfile, fieldnames)
all_lines = list(reader)
jsonfile.write ('{"people":[')
for row in all_lines:
jsonfile.write('{"person":')
#address = my_get_address (...)
new_row = ["first_name", row['first_name'], "last_name", row['last_name'], "phone", row['phone'], 'address', '123 anystreet']
json.dump(new_row, jsonfile)
jsonfile.write('}')
# comma + newline for every person excep the last
if row != all_lines[-1]:
jsonfile.write (',\n')
jsonfile.write (']}')
JSON输出:
{
"people" : [
{
"person" : [
"first_name",
"joe",
"last_name",
"smith",
"phone",
"999-555-1212",
"address",
"123 anystreet"
]
},
{
"person" : [
"first_name",
"mary",
"last_name",
"jones",
"phone",
"888-555-1212",
"address",
"123 anystreet"
]
}
]
}
解决方案
我假设您的 .csv 文件格式如下:
first_name,last_name,phone
abc,abc,123
def,def,456
ghi,ghi,789
然后你可能想要这样做(使用你的地址示例):
import csv
import json
def csv_to_json(csvFilePath, jsonFilePath):
jsonArray = []
# Read csv file
with open(csvFilePath, encoding='utf-8') as csvf:
# Load csv file data using csv library's dictionary reader
csvReader = csv.DictReader(csvf)
# Convert each csv row into python dict
for row in csvReader:
# Update row with new fields
row.update({'address': '123 anystreet'})
# Add this python dict to json array
jsonArray.append(row)
# Convert python jsonArray to JSON String and write to file
with open(jsonFilePath, 'w', encoding='utf-8') as jsonf:
jsonString = json.dumps(jsonArray, indent=2)
jsonf.write(jsonString)
csvFilePath = r'test.csv'
jsonFilePath = r'test.json'
csv_to_json(csvFilePath, jsonFilePath)
生成的 .json 文件:
[
{
"first_name": "abc",
"last_name": "abc",
"phone": "123",
"address": "my_address"
},
{
"first_name": "def",
"last_name": "def",
"phone": "456",
"address": "my_address"
},
{
"first_name": "ghi",
"last_name": "ghi",
"phone": "789",
"address": "my_address"
}
]
编辑
要像您的问题一样格式化输出,您可以将这两行更改为换行row
,jsonArray
这样:
jsonArray.append({'person': row})
jsonString = json.dumps({'people': jsonArray}, indent=2)
现在它给出:
{
"people": [
{
"person": {
"first_name": "abc",
"last_name": "abc",
"phone": "123",
"address": "my_address"
}
},
{
"person": {
"first_name": "def",
"last_name": "def",
"phone": "456",
"address": "my_address"
}
},
{
"person": {
"first_name": "ghi",
"last_name": "ghi",
"phone": "789",
"address": "my_address"
}
}
]
}
推荐阅读
- android - 如何为小于 24 的 API 添加 Android 网络安全配置
- java - 在定义为 JLabel 文本的希伯来语字符串中使用 html 标记 使程序加载速度变慢
- forms - 母版页未打开(Xamarin 表单)
- c++ - 创建了一个链表,但是如何在 C++ 中删除“所有”节点
- python - 如何与 Outlook 中的特定电子邮件进行交互?
- javascript - 这可以即时编码/解码上传的文件吗?
- python - 使用嵌入在python字典列表中的元组列表
- c# - C#类型转换,泛型应该如何实现?
- constructor - Ue4 在运行时使用播放器启动和物理设置组件位置
- dialogflow-es - 从口语列表中获取所选项目的最佳选择?