使用python3将CSV转换为JSON

2024-10-03 13:25:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个这样的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文件的单个单元格中,前面的单元格为空。在

^{pr2}$

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

输出应该是这样的:

{
    "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格式。在


Tags: andofthetoincomforwith
2条回答

根据评论,我想这就是你的csv的样子:

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

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

^{pr2}$

内容文件.json公司名称:

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

您想在文件Article_Data.csv中漂亮地打印json。
文件名有误导性,不是'CSV。在

Python»文档:JSON encoder and decoder
漂亮的印刷:

>>> import json
>>> my_json_dict = json.load(open('Article_Data.csv'))
>>> print json.dumps(my_json_dict, sort_keys=True,
               indent=4, separators=(',', ': '))
{
    "article_id": 724465,
    "title": "Addition of Aripiprazole Ups Major Depressive Disorder Remission",
    "posting_time": 1499934606065,
... omited for brevity

相关问题 更多 >