0

2 つの入力ファイルがinput.txtあり、datainput.txt. の 2 列目がinput.txtの 1 列目に一致するかどうかを確認し、一致する場合は、出力ファイルの関連する行の最後datainput.txtに配置します。orthodb_id

入力.txt:

5 21 218
6 11 1931
7 26 173

datainput.txt:

>21|95|28|5
Computer
>11|28|5|5
Cate 

コード.py:

import csv

with open('input.txt', 'rb') as file1:
    file1_data = dict(line.split(None, 2)[1::-1] for line in file1 if line.strip())

with open('data.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]])

これは私のコードで得られる出力です:

>21|95|28|5|5
>11|28|5|5|6
4

1 に答える 1