从命令行将参数传递给python脚本 [英] passing arguments to python script from command line

查看:208
本文介绍了从命令行将参数传递给python脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的是python和pandas的新手,我正在尝试在命令行中使用参数执行python脚本,但出现错误,这是我的脚本

I am really new to python and pandas, I am trying to execute a python script using arguments in the command line but I got an error, here is my script

 #!/usr/bin/python
 import sys, pandas as pd

 df1 = pd.read_table(sys.argv[0], sep="\t", header=0)
 df2 = pd.read_table(sys.argv[1], sep="\t", header=0)

 df_merge = pd.merge(left=df1, right=df2, left_on=sys.arg[2],  right_on=sys.arg[3])
 df_merge.to_csv(sys.arg[4], sep="\t")

我得到了以下错误:KeyError: u'no item named file.out',任何帮助都将被取消

And I got the following error: KeyError: u'no item named file.out', any help would be apreciated

我的命令行语句是:merge_files.py file1.out file2.out col1 col3 test

推荐答案

sys.argv[0]是脚本的名称,即merge_files.py.您可以通过在脚本的开头插入print(sys.argv)来查看此内容.尝试将所有索引增加1.

sys.argv[0] is the name of the script, i.e. merge_files.py. You can see this by inserting print(sys.argv) at the beginning of your script. Try increasing all your indices by 1.

这篇关于从命令行将参数传递给python脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆