简体   繁体   English

在pyinstaller中添加2个数据文件,在脚本编写的函数中作为变量使用

[英]Add 2 data files in pyinstaller and use them as variables in the function written in the script

My aim is to create a one executable file using pyinstaller by converting a .py or .ipynb file.我的目标是通过转换 .py 或 .ipynb 文件使用 pyinstaller 创建一个可执行文件。 I'd like the user to enter their excel file pathway(2 files) when they run the exe file in their computer.我希望用户在他们的计算机中运行 exe 文件时输入他们的 excel 文件路径(2 个文件)。 The exe should then run my written python code which takes in their data files entered as my function arguments for data manipulation purpose and return's my function output(manipulated csv file) to the end-user using this application.然后,exe 应该运行我编写的 python 代码,该代码接收作为我的函数参数输入的数据文件,用于数据操作,并将我的函数输出(操纵的 csv 文件)返回给使用此应用程序的最终用户。

def manipulated_data(df,df5,df10):
    df2=df.copy()


    df4=df2.pivot_table(columns=df2[df2.columns[6]],index=df2[np.r_[df2.columns[0:6],df2.columns[9:17],df2.columns[18:]]],values=df2[df2.columns[7:9]],fill_value='')

    df4=df4.reset_index()

    #Combining multi-index names
    df4.columns = [' '.join(col).strip() for col in df4.columns.values]




    df7=df5.melt(id_vars=df5[df5.columns[0:5]],value_vars=df5[df5.columns[6:15]],value_name='Values')

    df7=df7.replace('Zone 9`','Zone 9')

    ind=df7.variable.unique()

    df8=df7.pivot_table(columns=df7[df7.columns[4:6]],
                    index=df7[df7.columns[0:4]]
                    , values=df7.columns[6],aggfunc=('first'),fill_value='')

    df8=df8.reindex(ind,axis=1,level=1)

    df8.columns = [' '.join(col).strip() for col in df8.columns.values]

    df8.reset_index(inplace=True)

    df9=pd.merge(df4,df8,how='inner',on=['Store ID','Store Name','State'])

    df9.fillna('',inplace=True)

    dfcoles=pd.concat([df9,df10], axis=0,ignore_index=False,sort=False)

    dfcoles.fillna('',inplace=True)

    coles_ind=df10.columns

    dfcoles=dfcoles.reindex(coles_ind,axis=1)

    return dfcoles

df_trial=pd.read_csv('C:/Users/Abdul.Qavi/Downloads/Day 14 data/Part 3.csv',keep_default_na=False)

df_part4=pd.read_csv('C:/Users/Abdul.Qavi/Downloads/Day 14 data/Part 4.csv',keep_default_na=False)

df_coles=pd.read_csv('C:/Users/Abdul.Qavi/Downloads/Coles Service SF.csv',keep_default_na=False)
 ##This file wouldn't be entered by the user. It would always be a part of the script.

dfx=manipulated_data(df_trial,df_part4,df_coles)

Based on my understanding of the question根据我对问题的理解

  • you want the user to input the path for 2 csv files您希望用户输入 2 个 csv 文件的路径
  • read the files and pass it as an argument读取文件并将其作为参数传递
  • perform some operations using manipulated_data() function使用handled_data() 函数执行一些操作
  • return/save the output - say in a csv file format.返回/保存输出 - 以 csv 文件格式说。

The general version would look like this -一般版本看起来像这样 -

app.py应用程序

import pandas as pd
import os

def manipulated_data(df1,df2):
    #perform operations using df1 and df2
    #......
    #......
    #for example - 
    df_coles = pd.concat([df1,df2], axis=0,ignore_index=False,sort=False) 
    return df_coles

# get file paths from the user
path1 = input("enter path for the 1st file")
path2 = input("enter path for the 2nd file")

#read the csv files
data1 = pd.read_csv(path1)
data2 = pd.read_csv(path2)

#execute manipulated_data() function-
df_final = manipulated_data(data1,data2)

#create an output directory
if not os.path.exists('output'):
    os.makedirs('output')

#save csv file in the output directory or your prefered location
df_final.to_csv('./output/manipulated_data1.csv') 

# Open excel to display the csv file
os.system("start excel output/manipulated_data1.csv")

# To prevent the app from closing abruptly -
input("Press enter to continue...")

And then create the application -然后创建应用程序 -

pyinstaller app.py

Side note - You can use os.getcwd() to read files from or save the file to the current directory.旁注 - 您可以使用 os.getcwd() 从当前目录读取文件或将文件保存到当前目录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM