1

这就是我试图转换为 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";

            }

        }

    };
4

2 回答 2

3

看起来可以这样做:

my_type = 500
for row, value in filearray:
   if row == member:
      my_type = value
      print row, my_type
      break
于 2012-06-21T12:46:44.627 回答
1

我不确定你想要什么mtype,因为它的初始值500被覆盖了(除非你在循环中进一步使用它)。我只是假设您需要保留collection中 each的500值。mtypememberfiles

但是为了使您的代码更正确:

for member in files:
        mytype = 500
        x = 0 #if you really need x variable here
        for file in filearray:
            x += 1
            if member in file[0]:
                mytype = file[1]
                print(x, mytype, member)
                break
于 2012-06-21T12:47:01.307 回答