我有兩個文件,我想保留文件1的結構(使行保留「>」),刪除文件1和文件2中的非常見行。示例:刪除兩個文件中的罕見行,但保留文件結構
文件1:
>countries
France
Italy
>cities
Rome
文件2:
France
希望的輸出(消除罕見線,保持爲 「>」 行):
>countries
France
>cities
任何想法?
我有兩個文件,我想保留文件1的結構(使行保留「>」),刪除文件1和文件2中的非常見行。示例:刪除兩個文件中的罕見行,但保留文件結構
文件1:
>countries
France
Italy
>cities
Rome
文件2:
France
希望的輸出(消除罕見線,保持爲 「>」 行):
>countries
France
>cities
任何想法?
試試這個
with open("f1.txt") as file1:
file1Lines = file1.readlines()
with open("f2.txt") as file2:
countriesToKeep = file2.readlines()
file1Lines = [i.strip() for i in file1Lines]
countriesToKeep = [i.strip() for i in countriesToKeep]
newFileLines = []
for line in file1Lines:
if line.startswith(">"):
newFileLines.append(line)
elif line in countriesToKeep:
newFileLines.append(" "+line)
with open("output.txt", "w") as output:
for line in newFileLines:
output.write(line+"\n")
我會加載你想保留在列表中的國家,以及你在列表中經歷的文件。如果該行中有>
字符,請將其附加到新的輸出列表中,或者該行在File 2
中,然後將其附加到輸出列表中。然後遍歷輸出列表中的每個項目並將其寫入輸出文件。
with open("test.txt", "r") as file1:
file1Lines = file1.readlines()
with open("test2.txt", "r") as file2:
countriesToKeep = file2.readlines()
newFileLines = []
for line in file1Lines:
if ">" in line:
newFileLines.append(line)
elif line.strip() in countriesToKeep:
newFileLines.append(line)
with open("output.txt", "w") as output:
for line in newFileLines:
output.write(line)
也許這樣嗎?
我的方法是從第二個文件創建一個要保留的行的列表。該列表包含該文件中不以>
開頭的行(不確定是否允許第二個文件包含>
)。然後遍歷第一個文件,並只採用以>
開頭的行或來自Keepers列表的行。
with open('file1.txt') as f1, open('file2.txt') as f2:
keepers = {line.strip() for line in f2 if not line.startswith('>')}
lines = [line.strip() for line in f1]
with open('out.txt', 'w') as fout:
for line in lines:
if line.startswith('>'):
fout.write(line + '\n')
elif line in keepers:
fout.write(' ' + line + '\n')
一行代碼版本:
print '\n'.join(filter(lambda x: x.startswith('>') or x in map(lambda x: x.strip(), open('file2.txt').readlines()), map(lambda x: x.strip(), open('file1.txt').readlines())))
使用'與open',以避免'.close()'。 'line.startswith(「>」)'更好? – itzMEonTV
@itzmeontv changed – heinst
hello heinst,它只輸出「>」行:「> countries > cities」 – Peaceandlove