If you have any doubt, feel free to contact me at Twitter or by e-mail eu at. Python json_to_csv.py input.txt output.csv Hi everybody, this is a simple snippet to help you convert your JSON file to a CSV file using a Python script.Ĭreate a new Python file like: json_to_csv.pyĪdd this code: import csv, json, sys #if you are not using utf-8 files, remove the next line sys.setdefaultencoding("UTF-8") #set the encode to utf8 #check if you pass the input file and output file if sys.argv is not None and sys.argv is not None: fileInput = sys.argv fileOutput = sys.argv inputFile = open(fileInput) #open json file outputFile = open(fileOutput, 'w') #load csv file data = json.load(inputFile) #load json content inputFile.close() #close the input file output = csv.writer(outputFile) #create a csv.write output.writerow(data.keys()) # header row for row in data: output.writerow(row.values()) #values rowĪfter adding this, save the file and run at the terminal: You may now use the following template to assist you in converting the JSON string to CSV using Python: import pandas as pd df pd.readjson (r'Path where the JSON file is saved\File Name.json') df.tocsv (r'Path where the new CSV file will be stored\New File Name.csv', index None) For our. Steps to Convert a JSON String to CSV using Python Step 1: Prepare a JSON String To start, prepare a JSON string that you’d like to convert to CSV. How to convert a JSON file to CSV - PYTHON SCRIPT Step 4: Convert the JSON String to CSV using Python.
0 Comments
Leave a Reply. |