gpt4 book ai didi

python - 使用 python 3 将 CSV 转换为 JSON

转载 作者:行者123 更新时间:2023-11-30 22:33:30 31 4
gpt4 key购买 nike

我有一个像这样的csv文件,没有标题,数据格式如下:

{"article_id":724465,"title":"Addition of Aripiprazole Ups Major Depressive Disorder Remission","posting_time":1499934606065,"archive_date":"11-Jul-2018","short_desc":"Small increased likelihood of remission at 12 weeks in patients with major depressive disorder","article_type":"Journal","article_body":[{"article_desc":"WEDNESDAY, July 12, 2017 (HealthDay News) -- For patients with major depressive disorder (MDD), augmentation with aripiprazole is associated with an increased likelihood of remission, according to a study published in the July 11 issue of the Journal of the American Medical Association. \n\n Somaia Mohamed, M.D., Ph.D., from the VA Connecticut Healthcare System in West Haven, and colleagues examined the relative effectiveness and safety of three treatments for MDD in patients unresponsive to at least one antidepressant course. Patients were randomly assigned to switch to a different antidepressant, bupropion (switch group, 511 patients); augment current treatment with bupropion (augment-bupropion group, 506 patients); or augment with an atypical antipsychotic, aripiprazole (augment-aripiprazole group, 505 patients) for 12 weeks and up to 36 weeks (acute treatment and continuous phases). \n\n The researchers found that at 12 weeks, the remission rates were 22.3, 26.9, and 28.9 percent for the switch, augment-bupropion, and augment-aripiprazole groups; the augment-aripiprazole group exceeded the switch group in remission (relative risk, 1.3). The augment-aripiprazole group had greater response compared with the switch or augment-bupropion groups (74.3 versus 62.4 percent [relative risk, 1.19] and 65.6 percent [relative risk, 1.13], respectively). There were no significant treatment differences for relapse. The bupropion groups had more anxiety (24.3, 22.5, and 16.6 percent in the switch, augment-bupropion, and augment-aripiprazole groups, respectively). \n\n Given the small effect size and adverse effects associated with aripiprazole, further analysis including cost-effectiveness is needed to understand the net utility of this approach, the authors write. \n\n Several authors disclosed financial ties to pharmaceutical companies, including Bristol-Myers Squibb, which provided aripiprazole for use in the study.","links":[{"link_name":"Abstract/Full Text (subscription or payment may be required)","link_url":"http://jamanetwork.com/journals/jama/article-abstract/2643308"},{"link_name":"Editorial (subscription or payment may be required)","link_url":"http://jamanetwork.com/journals/jama/article-abstract/2643294"}]}],"long_desc":"For patients with major depressive disorder, augmentation with aripiprazole is associated with an increased likelihood of remission, according to a study published in the July 11 issue of the Journal of the American Medical Association.","content_url":"http://jamanetwork.com/journals/jama/article-abstract/2643294","large_image":"https://s3-ap-southeast-1.amazonaws.com/ebiz-env-qa/content/healthday/article-images/original/24ca07f28e0db094d35038ab62df7ac5?AWSAccessKeyId=AKIAI5RP6RWRC7U5V54A&Expires=1798705806&Signature=JgcWiK1lnRnhqTDUtS%2BS9mp%2BVUI%3D","medium_image":"https://s3-ap-southeast-1.amazonaws.com/ebiz-env-qa/content/healthday/article-images/medium/24ca07f28e0db094d35038ab62df7ac5?AWSAccessKeyId=AKIAI5RP6RWRC7U5V54A&Expires=1798705806&Signature=BLZKa1YPV86ZigzxJsSx9hEchm8%3D","small_image":"https://s3-ap-southeast-1.amazonaws.com/ebiz-env-qa/content/healthday/article-images/small/24ca07f28e0db094d35038ab62df7ac5?AWSAccessKeyId=AKIAI5RP6RWRC7U5V54A&Expires=1798705806&Signature=pgnNhk12ijVV9EqmN9fRFqgatxU%3D","micro_image":"http://s3-ap-southeast-1.amazonaws.com/ebiz-env-qa/content/healthday/article-images/micro/health-day-micro.png","copyright":"http://www.healthday.com/","topics":["Depression","Prescription Drugs"],"speciality":["Acupuncture","Ayurveda","Family Medicine","Homeopathy","Nutrition","Occupational Therapy","Yoga","Other","Internal Medicine","Geriatrics","Epidemology","General Medicine","Transfusion Medicine","Nursing","Pharmacology","Psychiatry","Psychology","Sexology"]}

其中每条记录都位于 csv 文件的单个单元格中,并且前面的单元格为空。

csvfile = open('Article_Data.csv', 'r')
jsonfile = open('file.json', 'w')

reader = csv.DictReader(csvfile)
for row in reader:
json.dump(row, jsonfile,indent=4)

显示错误。有什么想法吗? csv 中的每条记录都是一个字典。

输出应该是这样的:

{
"article_id": 724465,
"title": "Addition of Aripiprazole Ups Major Depressive Disorder Remission",
"posting_time": 1499934606065,
"archive_date": "11-Jul-2018",
"short_desc": "Small increased likelihood of remission at 12 weeks in patients with major depressive disorder",
"article_type": "Journal",
"article_body": [
{
"article_desc": "WEDNESDAY, July 12, 2017 (HealthDay News) -- For patients with major depressive disorder (MDD), augmentation with aripiprazole is associated with an increased likelihood of remission, according to a study published in the July 11 issue of the Journal of the American Medical Association. \n\n Somaia Mohamed, M.D., Ph.D., from the VA Connecticut Healthcare System in West Haven, and colleagues examined the relative effectiveness and safety of three treatments for MDD in patients unresponsive to at least one antidepressant course. Patients were randomly assigned to switch to a different antidepressant, bupropion (switch group, 511 patients); augment current treatment with bupropion (augment-bupropion group, 506 patients); or augment with an atypical antipsychotic, aripiprazole (augment-aripiprazole group, 505 patients) for 12 weeks and up to 36 weeks (acute treatment and continuous phases). \n\n The researchers found that at 12 weeks, the remission rates were 22.3, 26.9, and 28.9 percent for the switch, augment-bupropion, and augment-aripiprazole groups; the augment-aripiprazole group exceeded the switch group in remission (relative risk, 1.3). The augment-aripiprazole group had greater response compared with the switch or augment-bupropion groups (74.3 versus 62.4 percent [relative risk, 1.19] and 65.6 percent [relative risk, 1.13], respectively). There were no significant treatment differences for relapse. The bupropion groups had more anxiety (24.3, 22.5, and 16.6 percent in the switch, augment-bupropion, and augment-aripiprazole groups, respectively). \n\n Given the small effect size and adverse effects associated with aripiprazole, further analysis including cost-effectiveness is needed to understand the net utility of this approach, the authors write. \n\n Several authors disclosed financial ties to pharmaceutical companies, including Bristol-Myers Squibb, which provided aripiprazole for use in the study.",
"links": [
{
"link_name": "Abstract/Full Text (subscription or payment may be required)",
"link_url": "http://jamanetwork.com/journals/jama/article-abstract/2643308"
},
{
"link_name": "Editorial (subscription or payment may be required)",
"link_url": "http://jamanetwork.com/journals/jama/article-abstract/2643294"
}
]
}
]
}

我可以对一条记录执行此操作,但无法对 csv 中的多条记录执行此操作。

error with program

错误是,第一行复制了n次,显示的不是json格式。

最佳答案

根据评论,我猜这就是您的 csv 的样子:

, {"some_json": "some_value"}
, {"another_json": "another_value"}

此代码忽略每行中的第一个空单元格,解析 json,并将其漂亮地打印到文件中。

import csv
import json

with open('test.csv', 'r') as rf:
reader = csv.reader(rf)
with open('file.json', 'w') as wf:
all_data = [json.loads(x[1]) for x in reader]
wf.write(json.dumps(all_data, indent=4))

file.json 的内容:

[
{
"some_json": "some_value"
},
{
"another_json": "another_value"
}
]

关于python - 使用 python 3 将 CSV 转换为 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45143663/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com