Pig pass tab(\t)作为动态CSV分析的参数

2024-04-26 23:25:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在分析文件中包含任何分隔符数据的数据文件(CSV,'TSV'\t';') 该方法适用于“,”和“;”,但不适用于制表符“\t”,我们如何将tab作为param传递给pig?在

python代码

delimiter = '\t'
cmd = 'pig -f sample.pig -p file='+data_file +' -p delimiter=' + delimiter
subprocess.Popen(cmd, shell=True, stderr=subprocess.STDOUT) 

^{pr2}$

我得到以下例外

2014-03-31 03:26:41,412 [main] INFO  org.apache.pig.tools.parameters.ParameterSubstitutionPreprocessor - The parameter: "delimiter= " cannot be parsed by Pig. Please double check it
2014-03-31 03:26:41,412 [main] INFO  org.apache.pig.tools.parameters.ParameterSubstitutionPreprocessor - Parser give the follow error message:
2014-03-31 03:26:41,413 [main] INFO  org.apache.pig.tools.parameters.ParameterSubstitutionPreprocessor - Encountered "<EOF>" at line 1, column 16.
Was expecting one of:
    <IDENTIFIER> ...
    <OTHER> ...
    <LITERAL> ...
    <SHELLCMD> ...

Tags: 文件数据orginfocmdmainapachetools
1条回答
网友
1楼 · 发布于 2024-04-26 23:25:10

此处不要使用shell;tab是shell的空白,不作为参数发送:

cmd = ['pig', '-f', 'sample.pig', '-p', 'file=' + data_file, '-p',
       'delimiter=' + delimiter]
subprocess.Popen(cmd, stderr=subprocess.STDOUT) 

请注意,我将shell保留为默认的False;当您可以直接调用pig时,不需要将此命令传递给shell。当shell留在False时,改为传入一个参数列表。在

即使如此,我认为你可能需要给pig序列\t(两个字符):

^{pr2}$

或使用原始字符串:

delimiter = r'\t'

如果这不起作用,您将不得不使用特殊的大小写;我只读取pig latin expressions reference,因此这是未经测试的,但是我将使用一个条件表达式和TAB作为命令行参数:

results = LOAD '$file' USING PigStorage('$delimiter' == 'TAB' ? '\t' : '$delimiter');

在Python中:

delimiter = 'TAB'

相关问题 更多 >