如何从csv到json创建嵌套字段?我查看了另一个stackoverflow,但它们并不是我想要格式化的。我有一个包含1列的数据集,我必须将其转换为嵌套字段。
数据:
ID, NAME
1, "Smith, Mr. Adams"
2, "McAdams, Mrs. Audrey"
3, "McAdams, Doctor John"
4, "Missing Value"
代码:
with open('test.csv', 'r') as file:
headers = next(file) #skip the headers
fieldnames = headers.rstrip().split(",")
csv_reader = csv.DictReader(file, fieldnames) #creating a dictionary
import datetime
for row_dict in csv_reader:
row_dict['name'] = row_dict['name'].split(",")
json_data = json.dumps(row_dict)
print(json_data)
我在列表中获取输出但它没有嵌套。
{"id": "1", "name": ["Smith", " Mr. Adams"]}
{"id": "2", "name": ["McAdams", " Mrs. Audrey"]}
{"id": "3", "name": ["McAdams", " Doctor John"]}
{"id": "4", "name": ["Missing Value"]}
最终输出:有没有办法做到这一点?
{"id": "1", "name": [{"last_name": "Smith",
"prefix": "Mr.",
"first_name": "Adams"}]}
{"id": "1", "name": [{"last_name": "McAdams",
"prefix": "Mrs.",
"first_name": "Audrey"}]}
{"id": "1", "name": [{"last_name": "McAdams",
"prefix": "Dr.",
"first_name": "John"}]}
{"id": "1", "name": [{"last_name": "Missing Value",
"prefix": "Missing Value",
"first_name": "Missing Value"}]}
答案 0 :(得分:1)
有时候只使用.split()
并创建一个新的词典。
import json
csv = '''1, "Smith, Mr. Adams"
2, "McAdams, Mrs. Audrey"
3, "McAdams, Doctor John"
4, "Missing Value"'''
csv_lines = csv.split('\n')
for line in csv_lines:
id = line.split(',')[0]
name = line[len(id)+3:-1]
split = name.split(', ')
last_name = split[0]
if len(split) < 2:
first_name = last_name
prefix = last_name
else:
prefix = split[1].split(' ')[0]
first_name = split[1][len(prefix)+1:]
row_dict = {'id': id, 'name': {'last_name': last_name, 'prefix': prefix, 'first_name': first_name}}
json_data = json.dumps(row_dict)
print(json_data)
输出:
{"id": "1", "name": {"last_name": "Smith", "prefix": "Mr.", "first_name": "Adams"}}
{"id": "2", "name": {"last_name": "McAdams", "prefix": "Mrs.", "first_name": "Audrey"}}
{"id": "3", "name": {"last_name": "McAdams", "prefix": "Doctor", "first_name": "John"}}
{"id": "4", "name": {"last_name": "Missing Value", "prefix": "Missing Value", "first_name": "Missing Value"}}