1

「>」で始まるバイオメトリクスのファイルを操作するスクリプトを作成しましたが、「>」のない同じテキストファイルがあり、スクリプトを変更しましたが、出力が表示されません。これは私のスクリプトです

with open('input1.txt', 'rb') as file1:
    file1_data = dict(line.split(None, 2)[1::-1] for line in file1 if line.strip())
with open('input2.txt', 'rb') as file2, open('output.txt', 'wb') as outputfile:
    output = csv.writer(outputfile, delimiter='|')
    for line in file2:
        if line[:1] == '>':
            row = line.strip().split('|')
            key = row[0][1:]
            if key in file1_data:
                output.writerow(row + [file1_data[key]])
        else:
            outputfile.write(line)

これは入力 1 です。

Q5 Bat Wood 
Q6 Ball Tough 
Q7 Pitch Dry

これはinput2でした:

>Bat|Batsman
>Ball|Bowler
>Pitch|Cricket

したがって、プログラムは input1 の 2 行目を input2 の 1 行目に一致させ、一致する場合は、次のように input1 の row1 を input2 にアタッチしますoutput

>Bat|Batsman|Q5
>Ball|Bowler|Q6
>Pitch|Cricket|Q7

しかしNow、私のinput2ファイルは

入力 2:

Bat|batsman 8
Ball|bowler 4
Pitch|matches 9

必要な出力

Bat|batsman|Q5 8
Ball|bowler|Q6 4
Pitch|matches|Q7 9
4

1 に答える 1