【问题标题】:CSV to JSON file conversion and savingCSV到JSON文件的转换和保存
【发布时间】: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


    【解决方案1】:

    我假设您正在使用 pandas 来处理 csv 文件。在这种情况下,解决方案非常简单。我已经修改了你的第一个函数,现在应用程序可以工作了。

    # Dumping a Json file from CSV
    def writeToJSONFile(path, fileName, data):
        data.to_json(path)
    

    【讨论】:

    • 谢谢,它运行良好,但输出 JSON 文件显示/添加我想知道为什么?我将其添加到问题中。
    • 我已经修改了我的答案。 Pandas 允许您将文件路径直接指定到 'to_json' 方法中。它会生成一个你想要的 csv 文件。
    猜你喜欢
    • 2015-05-11
    • 1970-01-01
    • 1970-01-01
    • 2017-07-11
    • 2020-07-13
    • 2021-07-28
    • 1970-01-01
    • 2019-08-12
    • 2021-02-11
    相关资源
    最近更新 更多