我在 CSV 文件(几百行)中获取了以下数据,我正在尝试将其转换为合理的 JSON,以将其发布到 Rest api 中 我已经完成了所需的最低限度的字段,但这就是我所得到的:
dateAsked,author,title,body,answers.author,answers.body,topics.name,answers.accepted
13-Jan-16,Ben,Cant set a channel ,"Has anyone had any issues setting channels. it stays at �0�. It actually tells me there are �0� files.",Silvio,"I�m not sure. I think you can leave the cable out, because the control works. But you could try and switch two port and see if problem follows the serial port. maybe �extended� clip names over 32 characters.
Please let me know if you find out!
Best regards.",club_k,TRUE
这是一个 JSON 示例,大致类似于我需要到达的位置:
json_test = """{
"title": "Can I answer a question?",
"body": "Some text for the question",
"author": "Silvio",
"topics": [
{
"name": "club_k"
}
],
"answers": [
{
"author": "john",
"body": "I\'m not sure. I think you can leave the cable out. Please let me know if you find out! Best regards.",
"accepted": "true"
}
]
}"""
Pandas 似乎可以将其导入数据帧(ish),但一直告诉我无法将其序列化为 json - 还需要对其进行清理和清理,但这应该在脚本中很容易实现。
在 Pandas 中也必须有一种方法可以做到这一点,但我在这里用头撞墙 - 因为答案和主题的列不能轻易地合并到 python 中的字典或列表中.
最佳答案
您可以使用 csv.DictReader
将 CSV 文件作为每行的字典进行处理。使用字段名称作为键,可以构建一个新字典,将公共(public)键分组到一个嵌套字典中,该嵌套字典由 .
之后的字段名称部分作为键。嵌套字典保存在一个列表中,尽管尚不清楚这是否真的有必要 - 嵌套字典可能会直接放置在顶层下方,而不需要列表。这是执行此操作的代码:
import csv
import json
json_data = []
for row in csv.DictReader(open('/tmp/data.csv')):
data = {}
for field in row:
key, _, sub_key = field.partition('.')
if not sub_key:
data[key] = row[field]
else:
if key not in data:
data[key] = [{}]
data[key][0][sub_key] = row[field]
# print(json.dumps(data, indent=True))
# print('---------------------------')
json_data.append(json.dumps(data))
对于您的数据,启用 print()
语句后,输出将是:
{ "body": "Has anyone had any issues setting channels. it stays at '0'. It actually tells me there are '0' files.", "author": "Ben", "topics": [ { "name": "club_k" } ], "title": "Cant set a channel ", "answers": [ { "body": "I'm not sure. I think you can leave the cable out, because the control works. But you could try and switch two port and see if problem follows the serial port. maybe 'extended' clip names over 32 characters. \nPlease let me know if you find out!\n Best regards.", "accepted ": "TRUE", "author": "Silvio" } ], "dateAsked": "13-Jan-16" } ---------------------------
关于python - 尝试在 python 中将 CSV 转换为 JSON 以发布到 REST API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38626039/