0

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

Yevhen Kuzmovych
  • 10,940
  • 7
  • 28
  • 48
Chinnari
  • 5
  • 3

1 Answers1

0

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)
mighty_mike
  • 714
  • 7
  • 19
  • No.....I definitely want to read the csv file through the config.json only.... – Chinnari Nov 23 '22 at 17:08
  • 1
    There's no such thing as "reading csv file through the config.json". You first read the config file and parse it properly to get the file name of the CSV file and then you read the CSV file, just like @mighty_mike suggested. If that's not what you need, please explain your problem in better detail. – Yevhen Kuzmovych Nov 23 '22 at 17:52
  • 1
    yah..the code was working...thank you...@mighty_mike – Chinnari Nov 24 '22 at 05:16