【发布时间】:2021-01-04 20:38:52
【问题描述】:
我正在尝试编写一个简单的 Tkinter 应用程序来读取 CSV 文件、清理它、转换为 JSON 格式并保存它。我在保存文件时遇到问题,错误与数据框对象不是 JSON 可序列化有关,我该如何解决这个问题,我在使用 python 进行数据科学和数据分析方面没有太多经验:
我的代码是
import tkinter as tk
from tkinter import filedialog
from tkinter import *
from tkinter import messagebox
from tkinter.filedialog import asksaveasfile
import pandas as pd
import numpy as np
import json
import csv
root = tk.Tk()
canvas1 = tk.Canvas(root, width=900, height=800, bg='lightsteelblue2', relief='raised')
canvas1.grid()
label1 = tk.Label(root, text='File Conversion Tool', bg='lightsteelblue2')
label1.config(font=('helvetica', 20))
canvas1.create_window(150, 60, window=label1)
# Initial set the Path to home
path = './'
# Dumping a Json file from CSV
def writeToJSONFile(path, fileName, data):
json.dump(data, path)
# Loading a CSV file
def getCSV():
global df
import_file_path = filedialog.askopenfilename()
df = pd.read_csv(import_file_path)
browseButton_CSV = tk.Button(text=" Import CSV File ", command=getCSV, bg='green', fg='white',
font=('helvetica', 12, 'bold'))
canvas1.create_window(150, 130, window=browseButton_CSV)
def storeJSON():
global df
global data
data = {}
data = df
print('This My Date before saving it to a JSON file')
print(data)
files = [('JSON File', '*.json')]
fileName = 'Myfile'
filepos = asksaveasfile(filetypes=files, defaultextension=json, initialfile='Myfile')
writeToJSONFile(filepos, fileName, data)
loadButton_CSV = tk.Button(text=" Store CSV as Json File ", command=storeJSON, bg='green', fg='white',
font=('helvetica', 12, 'bold'))
canvas1.create_window(150, 180, window=loadButton_CSV)
# Cleaning the loaded CSV file
def cleanCSV():
# Replace spaces with _ in the headers' names
df.columns = df.columns.str.replace(' ', '_')
# Dropping out duplicated records for the CSV files
df.drop_duplicates()
# Replacing any blank cell with NAN
df.replace(r'^\s*$', np.nan, regex=True)
CleanCSVButton_JSON = tk.Button(text='Clean CSV File', command=cleanCSV, bg='green', fg='white',
font=('helvetica', 12, 'bold'))
canvas1.create_window(150, 230, window=CleanCSVButton_JSON)
# Existing the Application
def exitapplication():
MsgBox = tk.messagebox.askquestion('Exit Application', 'Are you sure you want to exit the application',
icon='warning')
if MsgBox == 'yes':
root.destroy()
exitButton = tk.Button(root, text=' Exit Application ', command=exitapplication, bg='brown', fg='white',
font=('helvetica', 12, 'bold'))
canvas1.create_window(150, 280, window=exitButton)
root.mainloop()
我的错误是
TypeError: Object of type DataFrame is not JSON serializable
我希望我的输出是 JSON 文件格式。
我的输出文件包含一个奇怪的字符 \ 节点我使输出比实际文件短
"{\"{\\\"ACTIVITY_DATE\\\":{\\\"0\\\":\\\"8\\\\\\/23\\\\\\/2018\\\"\":{},\"1:\\\"12\\\\\\/6\\\\\\/2017\\\"\":{},\"2:\\\"6\\\\\\/23\\\\\\/2017\\\"\":{},\"3:\\\"3\\\\\\/19\\\\\\/2019\\\"\":{},\"4:\\\"3\\\\\\/1\\\\\\/2018\\\"\":{},\"5:\\\"10\\\\\\/18\\\\\\/2018\\\"\":{},\"6:\\\"2\\\\\\/9\\\\\\/2017\\\"\":{},\"7:\\\"2\\\\\\/26\\\\\\/2019\\\"\":{},\"8:\\\"9\\\\\\/6\\\\\\/2017\\\"\":{},\"9:\\\"3\\\\\\/29\\\\\\/2017\\\"}\":{},\"OWNER_ID:{\\\"0\\\":\\\"OW0000809\\\"\":{},\"1:\\\"OW0000809\\\"\":
..............................................................
{},\"2:\\\"OW0000809\\\"\":{},\"3:\\\"OW0000809\\\"\":{},\"4:\\\"OW0000002\\\"\":{},\"5:\\\"OW0000002\\\"\":{},\"6:\\\"OW0000002\\\"\":{},\"7:\\\"OW0000002\\\"\":{},\"8:\\\"OW0000002\\\"\":{},\"9:\\\"OW0000010\\\"}\":{},\"OWNER_NAME:{\\\"0\\\":\\\"31\\\"\":{},\"1:\\\"31\\\"\":{},\"2:\\\"31\\\"\":{},\"1:25719\":{},\"2:25719\":{},\"3:25719\":{},\"4:24032\":{},\"5:24032\":{},\"6:24032\":{},\"7:24032\":{},\"8:24032\":{},\"9:22723}}\":{}}"
【问题讨论】:
标签: python json dataframe tkinter