Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Convert columns in (.csv) file to (.json) column array in Python

I am trying to convert a (.csv) file to a .json file which is stored in the form of a column array.

The input (.csv) file is:

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

This is my desired result:

{
   "wavelength":[0.033962528,0.035974933,0.03801894,0.039994474,0.041975898,0.043954162],
   "n":[0.842171,0.83072,0.819753,0.809997,0.802291,0.797737],
   "k":[0.090738197,0.10934279,0.13025372,0.15338756,0.17980019,0.20882868],
   "alpha":[33573761.42,38194428.97,43052660.58,48194781.27,53826980.05,59703529.05],
   "absorption_length":[2.98e-8,2.62e-8,2.32e-8,2.07e-8,1.86e-8,1.67e-8]
}

I am using this script:

import csv
import json

with open('test.csv') as infile:
    reader = csv.DictReader(infile)
    out = [{"wavelength": row['wavelength'],"n": row["n"], "k": row["k"],, "alpha": ["alpha"]} for row in reader]

with open('data.json', 'w') as outfile:
    json.dump(out, outfile)

But I am getting the following:

[{"wavelength": "0.033962528", "n": "0.842171", "k": "0.090738197", "alpha": ["alpha"]}, 
{"wavelength": "0.035974933", "n": "0.83072", "k": "0.10934279", "alpha": ["alpha"]}, 
{"wavelength": "0.03801894", "n": "0.819753", "k": "0.13025372", "alpha": ["alpha"]},
 {"wavelength": "0.039994474", "n": "0.809997", "k": "0.15338756", "alpha": ["alpha"]}, 
{"wavelength": "0.041975898", "n": "0.802291", "k": "0.17980019", "alpha": ["alpha"]}, 
{"wavelength": "0.043954162", "n": "0.797737", "k": "0.20882868", "alpha": ["alpha"]}]

I am trying to get the desired result.

>Solution :

You can do it this way

import csv
import json

with open('test.csv') as infile:
    reader = csv.DictReader(infile)
    out = [{"wavelength": row['wavelength'],"n": row["n"], "k": row["k"], "alpha": ["alpha"]} for row in reader]
keys = out[0].keys()
ref_out = {key: [i[key] for i in out ] for key in keys} 


    
with open('data.json', 'w') as outfile:
    json.dump(ref_out, outfile)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading