to extract the data in a csv file which is given in the config.json file

171 views Asked by At

i have a config.json file and the data inside the config.json is ""

{
    "mortalityfile":"C:/Users/DELL/mortality.csv"
    
}

and the mortality file is a csv file with some data..i want to extract the csv file data from the cofig.json.The code which i wrote is

js = open('config.json').read()
results = []
for line in js:

    words = line.split(',')
    results.append((words[0:]))
print(results)

and i am geeting the output as the sourcefilename which i given..

[['{'], ['\n'], [' '], [' '], [' '], [' '], ['"'], ['m'], ['o'], ['r'], ['t'], ['a'], ['l'], ['i'], ['t'], ['y'], ['f'], ['i'], ['l'], ['e'], ['"'], [':'], ['"'], ['C'], [':'], ['/'], ['U'], ['s'], ['e'], ['r'], ['s'], ['/'], ['D'], ['E'], ['L'], ['L'], ['/'], ['m'], ['o'], ['r'], ['t'], ['a'], ['l'], ['i'], ['t'], ['y'], ['.'], ['c'], ['s'], ['v'], ['"'], ['\n'], [' '], [' '], [' '], [' '], ['\n'], ['}']]

i want to extract the data which is stored in the csv file through config.json in the python

1

There are 1 answers

3
mighty_mike On BEST ANSWER

I think you are confusing reading your .csv and reading your .json files.

import json

# open the json
config_file = open('config.json')

# convert it to a dict
data = json.load(config_file)

# open your csv
with open(data['mortalityfile'], 'r') as f:
    # do stuff with you csv data
    csv_data = f.readlines()
    result = []
    for line in csv_data:
        split_line = line.rstrip().split(',')
        result.append(split_line)

print(result)