1

i have a csv file like this, with no headers and the data format is as follows:

{"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"]}

where each record is in single cell of the csv file and the preceding cell is empty.

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)

it shows error. so any ideas? each record in csv is a dict.

this is how the output should be :

{
    "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"
                }
            ]
        }
    ]
}

i could do this for one record, but could not do for multiple record in the csv.

error with program

the error is, the first row is copied n number of times and the display is not in json format.

7
  • 8
    Your input example is already a valid json and not a csv file. Commented Jul 17, 2017 at 11:59
  • 1
    Try to reduce your input to something manageable. Don't dump hundreds of characters on us. You can reproduce the problem with a few dozen characters. Commented Jul 17, 2017 at 12:01
  • Is your example data all in one cell of the CSV and the preceeding cell is empty? And there are many rows exactly the same? Commented Jul 17, 2017 at 12:10
  • 1
    what error do you get? Commented Jul 17, 2017 at 12:11
  • @SuperShoot yes it is in single and preceding is empty. all the rows are like that, each record in the first column of that row. Commented Jul 17, 2017 at 12:19

2 Answers 2

3

I guess this is what your csv looks like based on comments:

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

This code ignores the first empty cell in each row, parses the json, pretty prints it to file.

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))

Contents of file.json:

[
    {
        "some_json": "some_value"
    },
    {
        "another_json": "another_value"
    }
]
Sign up to request clarification or add additional context in comments.

4 Comments

That's not valid JSON output
it is the contents of a file that json is written to, separated by newline.OP didn't detail how it should be output but I agree that given the file extension is .json the file should be valid json.. I'll edit.
thanks @SuperShoot , only thing was , the cell it should be accessing is column number '0' and not '1'. i edited the code. thank you. i will understand this logic very well.
thanks @SuperShoot , only thing was , the cell it should be accessing is column number '0' and not '1'. i edited the code. thank you. i will understand this logic very well. and jus by the way , i only wanted to load the first cell and ignore other cells.
0

You want to pretty print the json in the File Article_Data.csv.
The File name is misleading, it's not a `CSV.

Python » Documentation: JSON encoder and decoder
Pretty printing:

>>> 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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.