python3中的解析器不通过argparse从命令行获取定界符值 [英] Parser in python3 does not take delimiter values from commandline via argparse
问题描述
我已经写了一个简单的脚本作为满足awk
/sed
要求的高级工具.在脚本中,我根据查询文件的一列中的值比较两个文件,然后从主文件中提取整个条目.该脚本允许您输入每个文件的列和定界符的值.
I have written a simple script as an advanced tool for my awk
/sed
requirements. In the script I compare two files on basis of values from one column of the query file and then extract whole entries from the master file. The script allows you to enter the values for columns and delimiters for each file.
问题在于,当从命令行给出时,脚本无法识别'delimiter'选项.
The problem is that the 'delimiter' options are not recognized by script when given from command line.
这是我的代码(部分):
Here is my code (partial):
##- - - - - - - -- - - - - - Arguments - - - - - - - - - - - - - -##
parser = argparse.ArgumentParser()
## Command line options
parser.add_argument("-m", "--master", dest="master", help="master file")
parser.add_argument("-q", "--query", dest="query", help="queries to be extracted")
parser.add_argument("-d", "--delimiter", dest="delimiter", default='\t', help="delimiter in master")
parser.add_argument("-p", "--position", dest="position", default='1', help="position/column of value in master")
parser.add_argument("-d2", "--delimiter2", dest="delimiter2", default='\t', help="delimiter in query")
parser.add_argument("-p2", "--position2", dest="position2", default='1', help="position/column of value in query")
args = parser.parse_args()
def Extractor(master, query):
out_file = ('%s_matched_%s' % (query,master))
fh_out = open(out_file, 'w')
query_set = () ## To unique query set
for i in query:
key = i.split('args.delimiter2')[int(args.position2)] ## Key is the value on which matching will be done
query_set.add(key)
因此,如您所见,我从命令行获取查询文件"定界符的选项,并通过argparse
在脚本中使用它们,但这是行不通的.仅当我在脚本中明确提及定界符时,它才起作用:
So as you see, I take options for the 'query file' delimiter from the command line and use them in the script via argparse
, but that does not work. It only works if I explicitly mention the delimiter in the script like:
key = i.split('\t')[args.position2] ## Key is the value on which matching will be done
我提供的命令行选项是:
The command line option I give is:
$ py3 ExtractHeaders_v01.py -m ABC.csv -q XYZ.list -d2 \t -d , -p 1 -p2 0
其中
-
ABC.csv
是要从中提取条目的主文件.- 第二列将用于匹配(
-p 1
) - 其分隔符为逗号(
-d ,
)
ABC.csv
is the master file from which to extract entries.- The second column will be used for matching (
-p 1
) - Its delimiter is comma (
-d ,
)
- 第一列将用于匹配(
-p2 0
) - 其分隔符是制表符(
-d2 \t
)
- The first column will be used for matching (
-p2 0
) - Its delimiter is tab (
-d2 \t
)
请帮助我理解为什么从命令行给出分隔符时脚本不使用分隔符的原因.
Please help me understand why the delimiters are not used by script when given from the command line.
推荐答案
您还可以通过按
Ctrl+V
,然后按引号引起来的Tab
,在* nix外壳程序(例如bash)中传递Tab
字符.单或双),即键入"
Ctrl+V
Tab
"
.You can also pass the
Tab
character in a *nix shell (bash for example) by pressingCtrl+V
followed byTab
enclosed in quotes (single or double), i.e. type"
Ctrl+V
Tab
"
.这篇关于python3中的解析器不通过argparse从命令行获取定界符值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- The second column will be used for matching (
- 第二列将用于匹配(