csv 在Jupyter Lab IDE中运行的Python代码无法在命令行中工作

thtygnil  于 2023-05-26  发布在  Python
关注(0)|答案(2)|浏览(195)

下面是我的代码

import pandas as pd
import os

def list_files(folder_path):
    files = []
    for file_name in os.listdir(folder_path):
        file_path = os.path.join(folder_path, file_name)
        if os.path.isfile(file_path):
            files.append(file_name)
    return files

file_path = input("Enter the output files path")
folder_path = input("Enter the input files path")
file_list = pd.Series(list_files(folder_path))
csv_data_source = []

for files in file_list:
    dfinv = pd.read_excel(folder_path + files,sheet_name="Import")
    dfscn = pd.read_excel(folder_path + files,sheet_name="ScannedPartNumbers")
    partmaster = pd.read_csv('part_master.csv',encoding='latin1',low_memory=False)
    dfscn = dfscn[dfscn['ScannedPartNumber'].str.len() != 71]
    dffnl = pd.DataFrame(columns = ['InvNo','PartNo','ScnSumQty','ScnAvgMRP','InvAvgMRP','InvGST','MRPDiff','TotDiff'])
    
    dfscn['scnpartno'] = dfscn['ScannedPartNumber'].str.slice(start=30,stop=48)
    dfscn['scnpartqty'] = dfscn['ScannedPartNumber'].str.slice(start=49,stop=55)
    dfscn['scnpartmrp'] = dfscn['ScannedPartNumber'].str.slice(start=56,stop=66)
    dfscn['scnpartno'] = dfscn['scnpartno'].str.strip()
    #dfscn['scnpartno'] = pd.to_char(dfscn['scnpartno'])
    dfscn['scnpartqty'] = pd.to_numeric(dfscn['scnpartqty'])
    dfscn['scnpartmrp'] = pd.to_numeric(dfscn['scnpartmrp'])
    dfinv['Qty'] = pd.to_numeric(dfinv['Qty'])
    dfinv['MRP'] = pd.to_numeric(dfinv['MRP'])
    unpano = pd.Series(dfscn['scnpartno'].unique())
    
    for pnum in unpano:
        if (dfscn.loc[dfscn['scnpartno'] == pnum,'scnpartmrp'].mean()) != (dfinv.loc[dfinv['Part #'] == pnum,'MRP'].mean()):
            dffnl.loc[pnum,'InvNo'] = files
            dffnl.loc[pnum,'PartNo'] = pnum
            dffnl.loc[pnum,'ScnSumQty'] = dfscn.loc[dfscn['scnpartno'] == pnum,'scnpartqty'].sum()
            dffnl.loc[pnum,'ScnAvgMRP'] = dfscn.loc[dfscn['scnpartno'] == pnum,'scnpartmrp'].mean()
            dffnl.loc[pnum,'InvAvgMRP'] = dfinv.loc[dfinv['Part #'] == pnum,'MRP'].mean()
            dffnl.loc[pnum,'InvGST'] = partmaster.loc[partmaster['Part Number'] == pnum,'GST'].mean()
            dffnl['MRPDiff'] = dffnl['ScnAvgMRP'] - dffnl['InvAvgMRP']
            dffnl['TotDiff'] = dffnl['MRPDiff'] * dffnl['ScnSumQty']
    dffnl.to_csv(file_path + files + ".csv",index = False)

print("Files generation successful")

for f1 in os.listdir(file_path):
    if f1.endswith('.csv'):
        f1 = os.path.join(file_path, f1)
        combineddf = pd.read_csv(f1)
        csv_data_source.append(combineddf)

merged_df = pd.concat(csv_data_source, axis = 0)
merged_df.to_csv(file_path + 'merged.csv',index=False)

print("Data merging successful")

代码在Jupyter Lab中正常运行。但它不是在命令行中运行。甚至没有给出任何错误。新的编码。快来人帮帮忙。
尝试在Jupyter实验室中编写Python代码并获得正确的输出。但同样不是从命令行运行。

bfhwhh0e

bfhwhh0e1#

从开始菜单打开anaconda promt,导航到项目文件夹,并使用以下命令运行包含上述代码的python文件。将myfile替换为您的文件名。

python myfile.py
7gcisfzg

7gcisfzg2#

在python文件(abc.py)中添加代码。从命令提示符运行它。

python abc.py

相关问题