python - 将 Perl For 循环转换为 Python

标签 python perl loops for-loop

这就是我正在尝试转换为 python 的内容

这是我有点迷路的地方

    foreach (@files) {
        if ($_ =~ /_histogram/) { next; }   #Exclude all the histogram files

        for($row = 0, $mytype = 500; $row < $filearray_count; $row++) {
                if ($_ eq $filearray[$row][0]) { 
                $mytype = $filearray[$row][1]; 
                print "$row, $mytype,  $_ \n";
                break;
        }}


        $myfile = $mydir.$_; 

        if ($mytype > 0) {
            open($in,  "<",  ($myfile)) or die "Can't open source file $myfile: $!";

            if ($mytype >= 500) { $mytype += ++$new_attribute; }
            #print "$row, $mytype,  $_ \n";
                    this while loop is confusing. Is it setting $Instring equal to $in?
            while ($inString = <$in>) {
                $inString =~ s/^\s*(.*)\s*$/$1/;
                if ($inString =~ /Feature File Version:/) { next; }
                if ($mytype eq 11 && $inString !~ /Subject:/) { next; }

                if ($mytype eq 11 && $inString =~ /Subject:/) { 

                    my($f1, $f2, $f3) = $inString =~ m/(.*\t)(.*\t)(.*)/;
                    #print $out $inString, "\n";
                    $f3 =~ s/(\\\d{3})/\^/g;
                    print $out $mytype, "\t", $f1, $f2, $f3, "\n";

                    foreach ($f3) {
                        push @f4, split(/(Subject:|In-Reply-To:|Reply-To:|To:|From:|Bcc:|Cc:|Host:|Date:|Distribution:)/i);
                    }

                    #print $out $mytype, "\t", $f1, "\t", "scalar=", $#f4+1, "\n";
                    $count = 0;
                    while ($count < $#f4) {

                        $f4[$count+2] =~ s/\^/ /g;
                        push @f5, substr(join('', $f4[$count+1], $f4[$count+2]), 0, 50);;
                        $count = $count + 2;

这部分很容易理解

                    print $out $mytype, "\t", $f1, $_, "\t", $f3, "\n" for @f5;
                    undef @f4;
                    undef @f5;

                    next; 
                }

不确定这里的替换。 $inString =~ s/(\\d{3})/\^/g; 打印 $out $mytype, "\t", $inString, "\n";

            }

        }

    };

最佳答案

看起来可以这样做:

my_type = 500
for row, value in filearray:
   if row == member:
      my_type = value
      print row, my_type
      break

关于python - 将 Perl For 循环转换为 Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11138297/

相关文章:

Python ml 引擎预测 : How can I make a googleapiclient. discovery.build 持久?

python - 未应用 Matplotlib 样式

loops - `forever` : How to forward information to next iteration?

java - 一个 while 循环,其中包含一个 if 语句和一个 for 循环

c - 我的 C 代码计算 k 的最小值,其总和大于用户输入 n 没有给我预期的结果?

Python - 为子类强制执行特定方法签名?

python - __main__.py 中是否需要 "if __name__ == ' __main_ _'"?

perl - 让 perl 调试器不会在第一条语句处停止

php - Perl 相当于 PHP_AUTH_PW

java - 有 OpenGrok API 吗?