将Perl转换为python3

时间:2019-06-18 20:59:10

标签: python-3.x perl comparison

我正在尝试将perl代码重新创建为python3代码。我没有足够的python专业知识来做到这一点。预先感谢。

我尝试(在python中)split()和其他东西,但是我什么也没有。

use strict;
use warnings;

open(FILE,"file.name.txt") or die;
while(<FILE>){
    chomp;
    my $name = $_;
    $name =~ s/\.Q20L20\.fq\.gz//;
    open(OUT,">$name.sh") or die;
    print OUT "#!/bin/bash\n";
    print OUT "tophat2 -output -input $name.Q20L20.fq.gz";
    print $name;
    close OUT;
 }
 close FILE;

perl脚本运行良好,但我无法将其放入Python

1 个答案:

答案 0 :(得分:1)

以下是Python 3中的示例:

import os
import re

with open( "file.name.txt") as fp:
    for line in fp:
        name = line.rstrip()
        name = re.sub(r'\.Q20L20\.fq\.gz', '', name)
        with open(name + '.sh', 'w') as fpw:
            fpw.write("#!/bin/bash" + os.linesep)
            fpw.write("tophat2 -output -input {}.Q20L20.fq.gz".format(name));
        print(name)