그래서 3 개의 CSV 파일을 열고 json으로 변환하고 '정리'한 다음 하나의 파일로 병합 할 수있는 프로그램을 설계했습니다.
내가 디자인 한 방식은 디스플레이에 각 파일을로드하기위한 3 개의 버튼이 있지만 사용자가 다른 변수에 할당 된 여러 파일을로드 할 수있는 1 개의 버튼 만 표시하는 더 나은 디자인이 있는지 궁금합니다. , 이러한 csv 파일의 coloum 제목에 따라 다릅니다.
내가 가진 문제는 하나의 파일을로드 할 때 이전에로드 된 파일을 재정의한다는 것입니다.
참고로 csv 파일은 1/3 형식 만 가능하므로 열 제목이 알려져 있습니다.
내 코드의 짧은 부분에서 볼 수 있듯이 두 개의 개별 파일에 대해 두 개의 다른로드 파일 버튼을 만들었지 만 다른 파일을 다른 변수로 지원하는 버튼 하나만 가질 수있는 방법이 있습니까?
import tkinter as tk
from tkinter import *
from tkinter import filedialog
from tkinter import messagebox
import pandas as pd
import json
import csv
import sys
import sqlite3
#
def loadInspection():
global inspectionFile
global label_inspectionFile
loadingCSV = filedialog.askopenfilename()
inspectionFile = pd.read_csv(loadingCSV)
#Making buttons for load and translate and clean
loadButton = Button(window1, text="Load Inspections File", command=loadInspection)
loadButton.grid(row=0, column=1)
#loading first file called inspections
def inspectionClean():
global inspectionFile
inspectionFile = inspectionFile
#Check and remove any duplicates in the Inspection Data
inspectionFile = inspectionFile[inspectionFile.duplicated() == False]
cleanInsButton = Button(window1, text="Clean", command=inspectionClean)
cleanInsButton.grid(row=0, column=3)
#loading second file called inventroy
def loadInventroy():
global inventroyFile
loadingCSV = filedialog.askopenfilename()
inventroyFile = pd.read_csv(loadingCSV)
#Making buttons for load and translate and clean
loadButton = Button(window1, text="Load Inventroy File", command=loadInventroy)
loadButton.grid(row=3, column=1)
def inventroyClean():
global inventroyFile
inventroyFile = inventroyFile
#Check and remove any duplicates in the Inspection Data
inventroyFile = inventroyFile[inventroyFile.duplicated() == False]
cleanInvButton = Button(window1, text="Clean", command=inventroyClean)
cleanInvButton.grid(row=3, column=3)
내 생각 엔 이것이 전역 변수에 문제가 될 수 있습니다. 일반적으로 인스턴스 / 클래스 변수를 대신 사용하는 것이 좋으며 전역 변수를 사용하는 것은 일반적으로 권장되지 않습니다.
로드 기능은 대신 다음과 같이 구성 될 수 있습니다.
class MyCSVLoader:
def __init__(self):
self.inventory = None
self.inspection = None
def loadCSV(self):
loadingCSV = filedialog.askopenfilename()
return pd.read_csv(loadingCSV)
def loadAll(self):
self.inspection = self.loadCSV()
self.inventory = self.loadCSV()
그러나 테스트 할 CSV 파일이 없기 때문에 이것이 작동하는지 알려주십시오.
그런 다음 다음과 같이 하나의 버튼으로 구현할 수 있습니다.
csvLoader = MyCSVLoader()
loadButton = tk.Button(window1, text="Load All Files", command=csvLoader.loadAll)
loadButton.grid(row=0, column=1)
2 개의 파일을 선택할 수 있도록 1 개의 창이 차례로 팝업됩니다. 선택한 파일은 다음을 통해 액세스 할 수 있습니다 csvLoader.inventory
와 csvLoader.inspection
.
클린 함수도 제대로 작동하려면이 클래스로 이동해야합니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다