关于python argparse
我正在尝试读取excel文件,并使用pandas.DataFrame将其输出为json 在这种情况下,我运行程序:关于python argparse,python,argparse,os.path,Python,Argparse,Os.path,我正在尝试读取excel文件,并使用pandas.DataFrame将其输出为json 在这种情况下,我运行程序: [y.pan@working excel_check]$ python3.6 check.py -j -o result.txt ./workingfile/testfile01.xlsx [y.pan@working excel_check]$ python3.6 check.py -j -o result.json ./workingfile/ 对于单个文件或目录的路径,它可
[y.pan@working excel_check]$ python3.6 check.py -j -o result.txt ./workingfile/testfile01.xlsx
[y.pan@working excel_check]$ python3.6 check.py -j -o result.json ./workingfile/
对于单个文件或目录的路径,它可以正常工作。但当我尝试这样输入数据时:
[y.pan@working excel_check]$ python3.6 check.py -j -o result.json ./workingfile/testfile*.xlsx
将始终获得错误:
unrecognized arguments: ./workingfile/testfile_01_fin.xlsx ./workingfile/testfile_02_fin.xlsx ./workingfile/testfile_03_fin.xlsx ... ./workingfile/testfile_99_fin.xlsx
下面是我试图计算的代码:
def make_df(input_path):
# path = os.getcwd()
df = pd.DataFrame()
if os.path.isdir(input_path):
files = os.listdir(input_path)
files_xlsx = [f for f in files if f[-4:] == "xlsx"]
for f in files_xlsx:
data = pd.read_excel(f)
df = df.append(data)
elif os.path.isfile(input_path):
data = pd.read_excel(input_path)
df = df.append(data)
else:
print("Not a file or directory.")
def create_argparser():
ap = argparse.ArgumentParser()
ap.add_argument(
"-o", "--output",
action = "store"
)
ap.add_argument(
"-j","--json",
action = "store_true"
)
ap.add_argument("input_path", action = "store")
return ap.parse_args()
def main():
args = create_argparser()
if args.json:
if args.output:
write_json(args.input_path, args.output)
else:
#write_json(df, stdout)
pass
else:
print_df(args.input_path)
# pass
exit()
我需要更改哪些内容才能运行第三个案例?
nargs='+'
-接受一个或多个。默认情况下,您的论点只接受一个path@h4z3谢谢但我还有一个问题,我的if&else无法读取我刚刚输入的列表。我应该怎么做?输入路径现在是一个列表,而不是一个字符串。您不能只执行例如os.path.isdir(输入路径)