Skip to main content
 首页 » 编程设计

python之尝试在 python 中将 CSV 转换为 JSON 以发布到 REST API

2024年10月17日8tintown

我在一个 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 似乎可以将其导入数据框(大概),但一直告诉我无法将其序列化为 json - 还需要对其进行清理和 sanitizer ,但这在脚本中应该很容易实现。

在 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" 
} 
---------------------------