英文:
Python code that is running in Jupyter Lab IDE is not working in command line
问题
以下是我的代码
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("输入输出文件路径")
folder_path = input("输入输入文件路径")
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['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("文件生成成功")
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("数据合并成功")
该代码在Jupyter Lab中正常运行。但在命令行中未运行,甚至没有报错。新手求帮助。尝试在Jupyter Lab中编写Python代码,并获得正确的输出。但是从命令行中运行时却无法正常工作。
英文:
Following is my code
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")
The code is properly running in Jupyter Lab. But it's not running in command line. Not even giving any error. New for coding. Somebody please help.
Tried to write a python code in Jupyter lab and getting correct output. But the same is not running from command line.
答案1
得分: 0
Add code in python file (abc.py). Run it from command prompt.
python abc.py
英文:
Add code in python file (abc.py). Run it from command prompt.
python abc.py
答案2
得分: 0
Open Anaconda Prompt 从开始菜单中打开,导航到项目文件夹,然后使用以下命令运行具有上述代码的 Python 文件。将 "myfile" 替换为您的文件名。
python myfile.py
英文:
Open anaconda promt from start menu, navigate to project folder and run the python file which has the above code using below command. Replace myfile with your file name.
python myfile.py
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论