It is a program to read a JSON file, aggregate the contents, and output it as CSV. Since the aggregated content changes depending on the usage, I am only making a place to read and output JSON.
--Display a file selection dialog --Read JSON file --Aggregate and output as CSV
Use tkinter
to display a GUI (file selection dialog).
As soon as you run it, a file selection dialog will appear, allowing you to select only JSON files.
#Module import
import os, tkinter, tkinter.filedialog
#Display file selection dialog
root = tkinter.Tk()
root.withdraw()
#Make only json files selectable
fTyp = [("","*.json")]
iDir = os.path.abspath(os.path.dirname(__file__))
file = tkinter.filedialog.askopenfilename(filetypes = fTyp, initialdir = iDir)
#Check the path of the selected file
print(file)
When you can select the file, check the contents of the JSON file.
Add the json
library to read and handle files.
#Module import
import os, json, tkinter, tkinter.filedialog
#Display file selection dialog
root = tkinter.Tk()
root.withdraw()
#Make only json files selectable
fTyp = [("","*.json")]
iDir = os.path.abspath(os.path.dirname(__file__))
file = tkinter.filedialog.askopenfilename(filetypes = fTyp, initialdir = iDir)
#Read json file
json_open = open(file, 'r', encoding="utf-8")
json_load = json.load(json_open)
#Check the contents of the json file
print(json_load[0])
Now that we have read the JSON file, let's make it output to CSV.
#Module import
import os, json, csv, tkinter, tkinter.filedialog
#Display file selection dialog
root = tkinter.Tk()
root.withdraw()
#Make only json files selectable
fTyp = [("","*.json")]
iDir = os.path.abspath(os.path.dirname(__file__))
#Get file information
file = tkinter.filedialog.askopenfilename(filetypes = fTyp, initialdir = iDir)
#Read json file
json_open = open(file, 'r', encoding='utf-8')
json_load = json.load(json_open)
#Set the folder and file name to output CSV (same name in the same location as the read JSON file)
folder_file = os.path.split(file)
folder = folder_file[0] #Get folder path
file_name = file.rsplit('/', 1)[1][0:-5] #Get the file name
csv_file_path = folder + '/' + file_name + '.csv'
#Create CSV file (create new if it does not exist)
if not os.path.isfile(csv_file_path):
open(csv_file_path, 'x', encoding='utf-8')
#Read CSV in overwrite mode
csv_file = open(csv_file_path, 'w', encoding='utf-8')
#Write settings to CSV
w = csv.writer(csv_file, lineterminator='\n')
#Write and write the aggregation process according to the contents of JSON
#w.writerow(['AAA','test','111'])← In the case of one line at a time
w.writerows([['AAA','Test','111'], ['BBB','tEst','222'], ['CCC','teSt','333'], ['DDD','tesT','444']])
csv_file.close()
that's all. After that, let's add up as you like.
-Python: How to select a processing file from GUI -Read JSON with Python -Python: Read JSON data from web API -Output to csv file with Python -[Python] Write to csv file with Python -The story that the "cp932" file could not be opened when reading the Json file with Python -About reading and writing (overwriting, adding) files -Set the line feed code of the file output by the Python csv module to LF
Recommended Posts