简体   繁体   中英

Convert multiple JSON files and print multiple csv files

I have some code that will take a bunch of JSON files and parse and convert them to csv. I have made it work by taking several JSON files (the output runs on command prompt) but I can't figure out how to make it print a csv file for each given JSON file it starts with.

So here is what I have. At the moment it works just fine one file at a time but as I have hundreds, it's necessary to automate it more so i can handle batches.

All help greatly appreciated. My attempts to piece other people's suggestions to this have not worked :/

import json

file_list = ['file.txt', 'file2.txt'] #insert filename(s) here
for x in range(len(file_list)):
    with open(file_list[x], 'r') as f:
        distros_dict = json.load(f)

    for distro in distros_dict:
        print (str(distro['timestamp'])+ ','+ str(distro['value']))

You can use pandas package.

import pandas as pd
pd.DataFrame.from_dict(distros_dict ['timestamp']['value'])

You dont need to use loop in json

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM