python3 中的解析器不会通过 argparse 从命令行获取分隔符值 [英] Parser in python3 does not take delimiter values from commandline via argparse

查看:19
本文介绍了python3 中的解析器不会通过 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.

问题是脚本无法识别命令行中的分隔符"选项.

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)

      请帮助我理解为什么从命令行给出分隔符时脚本不使用分隔符.

      Please help me understand why the delimiters are not used by script when given from the command line.

      推荐答案

      您还可以通过按 Ctrl+V<在 *nix shell(例如 bash)中传递 Tab 字符 后跟 Tab 括在引号中(单引号或双引号),即键入 " Ctrl+V Tab".

      You can also pass the Tab character in a *nix shell (bash for example) by pressing Ctrl+V followed by Tab enclosed in quotes (single or double), i.e. type " Ctrl+V Tab ".

      这篇关于python3 中的解析器不会通过 argparse 从命令行获取分隔符值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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