“Konvertieren Sie JSON in CSV” Code-Antworten

Python konvertieren JSON in CSV

import json
import csv
 
with open('G:\Akhil\jsonoutput.json') as json_file:
    jsondata = json.load(json_file)
 
data_file = open('G:\Akhil\jsonoutput.csv', 'w', newline='')
csv_writer = csv.writer(data_file)
 
count = 0
for data in jsondata:
    if count == 0:
        header = data.keys()
        csv_writer.writerow(header)
        count += 1
    csv_writer.writerow(data.values())
 
data_file.close()
Pleasant Porcupine

JSON an CSV Python

import pandas as pd

df = pd.read_json (r'my_file.json')
# content of my_file.json :
#
# {"Product":{"0":"Desktop Computer","1":"Tablet","2":"Printer","3":"Laptop"},"Price":{"0":700,"1":250,"2":100,"3":1200}}

df.to_csv (r'my_file.csv', index = None)
# content of my_file.json :
#
# Product,Price
# Desktop Computer,700
# Tablet,250
# Printer,100
# Laptop,1200
Plif Plouf

JSON zu CSV in Python

'''
@author : alixaprodev.com
'''
import csv, json
input_json_file='json_file.json'
output_csv_file='csv_file.csv'
input = open(input_json_file)
data = json.load(input)
input.close()
output = csv.writer(open(output_csv_file,'w'))
output.writerow(data[0].keys())  # header row
for row in data:
    output.writerow(row.values())
Pythonist

JSON zu CSV

# call csvkit (which is a Python tool) to convert json to csv:
# https://csvkit.readthedocs.io/en/latest/

in2csv data.json > data.csv
Troubled Tapir

CSV an JSON

python -c "import csv,json;print json.dumps(list(csv.reader(open('csv_file.csv'))))"
Thankful Thrush

Konvertieren Sie JSON in CSV

import json
if __name__ == '__main__':
    try:
        with open('input.json', 'r') as f:
            data = json.loads(f.read())

        output = ','.join([*data[0]])
        for obj in data:
            output += f'\n{obj["Name"]},{obj["age"]},{obj["birthyear"]}'
            
        with open('output.csv', 'w') as f:
            f.write(output)
    except Exception as ex:
        print(f'Error: {str(ex)}')
Harendra

Ähnliche Antworten wie “Konvertieren Sie JSON in CSV”

Fragen ähnlich wie “Konvertieren Sie JSON in CSV”

Weitere verwandte Antworten zu “Konvertieren Sie JSON in CSV” auf Python

Durchsuchen Sie beliebte Code-Antworten nach Sprache

Durchsuchen Sie andere Codesprachen