提取config.json文件中给定的csv文件中的数据

63lcw9qa  于 2022-11-26  发布在  其他
关注(0)|答案(1)|浏览(92)

我有一个config.json文件,config.json中的数据是“”

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

死亡率文件是一个包含一些数据的csv文件。我想从cofig.json中提取csv文件数据。我编写的代码是

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

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

我得到的输出是我给出的源文件名。

[['{'], ['\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'], ['}']]

我想通过python中的config.json提取存储在csv文件中的数据

c86crjj0

c86crjj01#

我认为您在阅读.csv文件和阅读.json文件时混淆了。

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)

相关问题