【问题标题】:Pipe python string to perl script管道 python 字符串到 perl 脚本
【发布时间】:2018-05-14 11:44:50
【问题描述】:

我想通过管道将文件路径从 Python 传输到 Perl 脚本。虽然我熟悉 Python 和 Bash,但我对 Perl 一无所知。
我有以下(示例)文件:

return.py

print( 'data/test.txt' )

uniprot.pl

use strict;
use warnings;
use LWP::UserAgent;

my $list = $ARGV[0]; # File containg list of UniProt identifiers.
my $base = 'http://www.uniprot.org';
my $tool = 'uploadlists';

my $contact = ''; # Please set your email address here to help us debug in case of problems.
my $agent = LWP::UserAgent->new(agent => "libwww-perl $contact");
push @{$agent->requests_redirectable}, 'POST';

my $response = $agent->post("$base/$tool/",
                            [ 'file' => [$list],
                              'format' => 'fasta',
                              'from' => 'ACC+ID',
                              'to' => 'ACC',
                            ],
                            'Content_Type' => 'form-data');

while (my $wait = $response->header('Retry-After')) {
  print STDERR "Waiting ($wait)...\n";
  sleep $wait;
  $response = $agent->get($response->base);
}

$response->is_success ?
  print $response->content :
  die 'Failed, got ' . $response->status_line .
    ' for ' . $response->request->uri . "\n";

当我从 shell 调用 perl 文件时:perl uniprot.pl data/test.txt 它工作正常。

我尝试了不同的方法来将 python 打印传递给这个调用,但显然是错误的:

1.

python3 return.py | perl uniprot.pl

这将给出:Failed, got 500 Internal Server Error for http://www.uniprot.org/uploadlists/。但是,据我所知,代码有效(如上所述),这一定是由错误的管道引起的。

2

python3 return.py | perl uniprot.pl -

这将给出:Can't open file -: No such file or directory at /usr/share/perl5/LWP/UserAgent.pm line 476. 所以似乎字符串被传递到 perl 文件,但是 perl 正在寻找一个完全不同的目录。

3
我更改了这一行:my $list = $ARGV[0]; --to--> my $list = <STDIN>; 然后再次调用上述两个命令(因此是 1 和 2)。两者都给:Can't open file data/test.txt : No such file or directory at /usr/share/perl5/LWP/UserAgent.pm line 476.


问题 如何将字符串从return.py 传递到uniprot.pl

【问题讨论】:

  • 绝对不是 Python 问题 - C 程序、shell 脚本或其他 perl 脚本也会遇到同样的问题。
  • 嗯,是的,回首过去,但我不确定我是否真的应该打印字符串还是应该不打印@brunodesthuilliers
  • 如果这是您程序的例外输出,那么打印它是正确的做法。

标签: shell perl


【解决方案1】:

您需要检查参数是通过命令行参数给出还是通过 STDIN 提供。

my $file;
if (@ARGV) {
    $file = $ARGV[0];
}
else {
    chomp($file = <STDIN>); # chomp removes linebreak
}

【讨论】:

  • 非常感谢!
  • 这只会给你第一行输入,不是吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-10-10
  • 1970-01-01
  • 2021-05-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多