[英]How to remove lines from a text file based the values in a list?
我有一個很大的文本文件(coverage.txt)> 2G,看起來像這樣:
#RefName Pos Coverage
BGC0000001_59320bp 0 0
BGC0000001_59320bp 1 0
BGC0000002_59320bp 2 0
BGC0000002_59320bp 3 0
BGC0000002_59320bp 4 0
BGC0000003_59320bp 5 0
BGC0000003_59320bp 6 0
BGC0000003_59320bp 7 0
BGC0000004_59320bp 8 0
BGC0000004_59320bp 7 0
BGC0000004_59320bp 8 0
BGC0000005_59320bp 7 0
BGC0000005_59320bp 8 0
BGC0000005_59320bp 7 0
BGC0000006_59320bp 8 0
BGC0000006_59320bp 7 0
BGC0000006_59320bp 8 0
BGC0000007_59320bp 7 0
BGC0000007_59320bp 8 0
BGC0000007_59320bp 7 0
BGC0000008_59320bp 8 0
BGC0000008_59320bp 7 0
BGC0000008_59320bp 8 0
BGC0000009_59320bp 7 0
BGC0000009_59320bp 8 0
我還有另一個文本文件(rmList.txt),如下所示:
BGC0000002
BGC0000004
BGC0000006
BGC0000008
如果行中包含rmList.txt中的ID,我想從我的coverage.txt文件中刪除這些行。
這是我嘗試過的:
wanted = [line.strip() for line in open('rmList.txt')]
files = 'coverage.txt'
def rmUnwanted(file):
with open(file) as f, open('out.txt', 'w') as s:
for line in f:
pos = line.split()[0].split('_')[0]
if pos not in wanted:
s.write(line)
rmUnwanted(files)
但是,這對於我的大文件來說是永遠的。 有一個更好的方法嗎? 我的代碼有什么問題嗎?
非常感謝!
使用set而不是list檢查重復的元素。
wanted = { line.strip() for line in open('rmList.txt') }
....
在我看來,代碼沒有錯,它可以滿足您的要求。 但是,大文件將需要時間。 您可能仍會提高效率。
如果您確定兩個文件都已排序(如您的示例所示),則此代碼應該更快:
def rmUnwanted(file):
with open(file) as f, open('out.txt', 'w') as s:
i = 0
lastwanted = ""
for line in f:
pos = line.split()[0].split('_')[0]
try:
if pos not in [wanted[i], lastwanted]:
s.write(line)
else:
if pos == wanted[i]:
lastwanted = wanted[i]
i = i+1
except IndexError:
s.write(line)
使用您提供的示例文件可以得到相同的結果,但是速度更快(我沒有測量,但是應該)。 我在這里要做的是避免在每次迭代時都在整個wanted
列表中查找pos
,如果您的實際rmList.txt也很大,這將很耗時。
您可以按照以下步驟進行操作:
with open("rmLst.txt") as f:
rmLst = set(f.readlines())
with open("out.txt", "w") as outf, open("coverage.txt") as inf:
# write header
outf.write(next(inf))
# write lines that do not start with a banned ID
outf.writelines(line for line in inf if line[:line.index("_")] not in rmList)
首先,將要刪除的所有ID存儲在一組中以進行快速查找。 然后,遍歷行並檢查每行是否以錯誤的ID開頭。 注意, line.split()
運行line.split()
我們還可以使用line[:line.index['_']]
檢查訪問每行的ID部分。 這樣可以避免創建每行的副本,並且應該比split
更快。 如果所有ID的長度都恆定,則可以用line.index['_']
替換line.index['_']
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.