将Perl For Loop转换为Python



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

            }
        }
    };

看起来可以这样做:

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

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

但要使您的代码更正确:

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
这是我

的Pythonizer为这段代码生成的内容(将"break"更改为"last"之后):

#!/usr/bin/env python3
# Generated by "pythonizer -m -v2 stack_11138297.pl" v0.967 run by JO2742 on Thu Mar 31 18:54:52 2022
import perllib, re, builtins
_str = lambda s: "" if s is None else str(s)
from perllib import Die
perllib.init_package("main")
f2 = None
count = 0
new_attribute = 0
inString = None
filearray_count = 0
f5 = perllib.Array()
files = perllib.Array()
filearray = perllib.Array()
f3 = None
out = ""
f1 = None
in_ = None
mydir = ""
x = ""
f4 = perllib.Array()
builtins.__PACKAGE__ = "main"
for _d in files:
    if re.search(r"_histogram", _str(_d)):
        continue
    # Exclude all the histogram files
    for row in range(0, (mytype := 500), filearray_count):
        if _str(_d) == _str(filearray[row][0]):
            mytype = filearray[row][1]
            perllib.perl_print(f"{row}, {mytype},  {_d} ")
            break
    myfile = mydir + _str(_d)
    if perllib.num(mytype) > 0:
        if not ((in_ := perllib.open_((myfile), "r"))):
            raise Die(f"Can't open source file {myfile}: {perllib.OS_ERROR}")
        if perllib.num(mytype) >= 500:
            mytype = perllib.num(mytype) + (new_attribute := new_attribute + 1)
        # print "$row, $mytype,  $_ n";
        # this while loop is confusing. Is it setting $Instring equal to $in?
        while inString := perllib.readline(in_):
            inString = re.sub(r"^s*(.*)s*$", r"g<1>", _str(inString), count=1)
            if re.search(r"Feature File Version:", _str(inString)):
                continue
            if _str(mytype) == _str(11) and not (
                re.search(r"Subject:", _str(inString))
            ):
                continue
            if _str(mytype) == _str(11) and (re.search(r"Subject:", _str(inString))):
                [f1, f2, f3] = perllib.list_of_n(
                    (
                        _m := re.search(r"(.*t)(.*t)(.*)", _str(inString)),
                        _m.groups() if _m else [],
                    )[1],
                    3,
                )
                # print $out $inString, "n";
                f3 = re.sub(re.compile(r"(\d{3})"), r"^", _str(f3), count=0)
                perllib.perl_print(mytype, "t", f1, f2, f3, "", file=out)
                for _d in f3:
                    f4.extend(
                        perllib.make_list(
                            perllib.split(
                                r"(Subject:|In-Reply-To:|Reply-To:|To:|From:|Bcc:|Cc:|Host:|Date:|Distribution:)",
                                _d,
                                flags=re.I,
                            )
                        )
                    )
                # print $out $mytype, "t", $f1, "t", "scalar=", $#f4+1, "n";
                count = 0
                while count < (len(f4) - 1):
                    perllib.substitute_element(
                        f4, count + 2, re.compile(r"^"), r" ", count=0
                    )
                    f5.extend(
                        perllib.make_list(
                            "".join(map(_str, (f4[count + 1], f4[count + 2])))[0:50]
                        )
                    )
                    count = count + 2
                    for _d in f5:
                        perllib.perl_print(mytype, "t", f1, _d, "t", f3, "", file=out)
                    f4 = perllib.Array()
                    f5 = perllib.Array()
                    continue
                inString = re.sub(
                    re.compile(r"(\d{3})"), r"^", _str(inString), count=0
                )
                perllib.perl_print(mytype, "t", inString, "", file=out)

最新更新