我已经使用 args 编写了下面的代码并添加了路径变量,但我无法测试此代码,因为我无法运行我的脚本。
有人可以帮忙在 jupyter 笔记本中运行这个脚本吗?
# Import Packages
import argparse
from pathlib import Path
import pandas as pd
import numpy as np
from datetime import datetime, timedelta, date,time
import sys
import os
import datetime
parser = argparse.ArgumentParser(description='Tractive_missing_campaign')
parser.add_argument("--SP_File",
help="Add path of your SP file",type=Path)
parser.add_argument("--File2",
help="Add path of your Datorama file",type=Path)
parser.add_argument("Missing_Campaign_File",help="Add path where you want to save your file",type=Path)
args = parser.parse_args()
## reading file path
Sp_Report = os.chdir(args.SP_File)
if not Sp_Report.exists():
raise ValueError(f"I did not find the SP file at {Sp_Report}")
try:
with open(Sp_Report, "r+") as csvfile:
file_df = pd.read_csv(csvfile)
except Exception as ex:
template = "An exception of type {0} occurred. Arguments:\n{1!r}"
message = template.format(type(ex).__name__, ex.args)
print(message)
## Datorama Report of Campaign keys
Report = os.chdir(args.File2)
if not Report.exists():
raise ValueError(f"I did not find the file at {Report}")
try:
with open(Report, "r+") as csvfile:
file_df2 = pd.read_csv(csvfile,usecols = ['Campaign Key'])
except Exception as ex:
template = "An exception of type {0} occurred. Arguments:\n{1!r}"
message = template.format(type(ex).__name__, ex.args)
print(message)
print(file_df2.shape)
output_path = args.Missing_Campaign_File
for slicer_column_value in slicer_column_values:
temporary_df = left_merged[(left_merged['currency'] == slicer_column_value) & (left_merged['Match'] == Match_Values)]
PartnerWiseFileName = os.path.join(output_path,'Missing_camp_{}_{}_{}_data.xlsx'.format(slicer_column_value, start_date, end_date))
print(temporary_df.shape)
temporary_df.to_excel(PartnerWiseFileName, index = False)
print("Job Complete")
有人可以让我知道我需要使用哪个查询来运行这个脚本吗?我正在使用下面提到的脚本
%%!
python Tractive_missing.ipynb --SP_File 'file1.csv' --Report2 'csv2'
如果您不关心从命令行实际运行它,您可以在笔记本顶部指定命令行参数。考虑这个示例单元格:
%%python - "Running with Arguments"
from argparse import ArgumentParser
parser = ArgumentParser()
parser.add_argument("message")
args = parser.parse_args()
print(args.message)
顶行将“Running with Arguments”作为第一个位置参数传递给 jupyter 笔记本,因此您可以调整该行以使用要用于笔记本的参数运行。然后,您还可以使用
jupyter execute
(https://docs.jupyter.org/en/latest/running.html#using-a-command-line-interface) 从命令行运行此示例,尽管这仍然不会'不允许您在命令行上专门指定参数,但仍然嵌入在笔记本本身中。如果您确实想在命令行中运行脚本,只需将其转换为正确的 python 脚本https://mljar.com/blog/convert-jupyter-notebook-python/