Как назначить вход из одного python сценария для входа в другой сценарий? - PullRequest
0 голосов
/ 15 января 2020

У меня сейчас два разных python скрипта. Первый преобразует файлы паркета в xlsx, а второй на самом деле просто пытается создать gui для запуска первого сценария. **

Вот первый сценарий:

import pandas as pd
import os
import pyarrow
import shutil
from pathlib import Path
file = input("What file would you like to convert from parquet to csv? ")
df1 = pd.read_parquet(file)
df = df1.append(df1, ignore_index=True)
dirout = input("Where would you like the xlsx file to be output to?")
name = input("What would you like to call the ouput file?" )
cfile = os.path.join(dirout, name + "." + "xlsx")
df.to_excel(cfile)

Во втором сценарии я бы хотел, чтобы пользователь ввел txt, который будет кормить файл, имя и dirout. Это возможно?

import os
from tkinter import *

window = Tk()
window.title("Convertor")
#Sets the size of the window
window.geometry('550x400')

#Adds a header to the window and configures the size 
lbl = Label(window, text="Convert Parquet to CSV", font=("Arial Bold", 18))

#Configures where the label message will appear
lbl.grid(column=0, row=0)

#adds an input text message
txt = Entry(window,width = 30)
txt.grid(column=3, row=3)

txt = Entry(window,width = 30)
txt.grid(column=4, row=4)
def clicked():
    os.system('python ParquetToCsv.py')
#Adding a button
btn = Button(window, text="Convert", command=clicked)
btn.grid(column=6, row = 6)

#The mainloop causes the window to remain open until someone interacts with it
window.mainloop()

1 Ответ

1 голос
/ 15 января 2020

Превратите ваш первый скрипт в функцию с аргументами для переменных, которые вы хотите передать:

def myfunc(f, n, dir):
   file = f
   df1 = pd.read_parquet(file)
   df = df1.append(df1, ignore_index=True)
   dirout = dir
   name = n
   cfile = os.path.join(dirout, name + "." + "xlsx")
   df.to_excel(cfile)

Затем импортируйте функцию в другой скрипт и вызовите ее, передав аргументы:

import os
from tkinter import *
from myutils import myfunc

window = Tk()
window.title("Convertor")
#Sets the size of the window
window.geometry('550x400')

#Adds a header to the window and configures the size 
lbl = Label(window, text="Convert Parquet to CSV", font=("Arial Bold", 18))

#Configures where the label message will appear
lbl.grid(column=0, row=0)

#adds an input text message
txt_name = Entry(window,width = 30)
txt.grid(column=3, row=3)

txt_file = Entry(window,width = 30)
txt.grid(column=4, row=4)

txt_dir = Entry(window,width = 30)
txt.grid(column=4, row=4)
def clicked():
    os.system('python ParquetToCsv.py')
#Adding a button
btn = Button(window, text="Convert", command=clicked)
btn.grid(column=6, row = 6)

#The mainloop causes the window to remain open until someone interacts with it
window.mainloop()

# Call function passing the arguments
myfunc(txt_file, txt_name, txt_dir)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...