我所能找到的就是如何洗掉特定單詞后的所有行。但我只想要特定數量的洗掉行。
例如,我有一個包含以下內容的檔案:
FCT
Paris
105,4
35
2,161 million
LZQ
London
1572
11
8,982 million
PRI
Paris
105,4
35
2,161 million
Rome
1285
11
2,873 million
PRI
Paris
105,4
35
2,161 million
現在我想洗掉巴黎之后的 3 行,巴黎之前的行和包含巴黎本身的行。
預期輸出將是:
LZQ
London
1572
11
8,982 million
只洗掉巴黎的方法:
bad_words = ['Paris',]
with open('DataSystem.txt') as oldfile, open('newfile.txt', 'w') as newfile:
for line in oldfile:
if not any(bad_word in line for bad_word in bad_words):
newfile.write(line)
uj5u.com熱心網友回復:
這很不優雅,但它有效,假設您想在遇到“壞詞”時正好洗掉前一行和后三行。如果有時在“壞詞”后面有更多行或更少行,它將無法按預期作業:
bad_words = {"Paris"} # membership tests with sets are O(1)
with open('DataSystem.txt') as oldfile:
data = oldfile.read().split("\n")
i = 0
new_data = []
while i < len(data):
item = data[i]
if item in bad_words:
del new_data[-1]
i = 4
continue
new_data.append(item)
i = 1
輸出:
['LZQ',
'London',
'1572',
'11',
'8,982 million',
'Rome',
'1285',
'11',
'2,873 million']
然后,您可以將其寫入您的newfile:
with open('newfile.txt', 'w') as newfile:
newfile.write("\n".join(new_data))
uj5u.com熱心網友回復:
這正是我所描述的。一次讀取 5 行檔案。如果在第 2 行中沒有發現“壞詞”,請寫出這 5 行。
bad_words = ['Paris']
with open('DataSystem.txt') as oldfile, open('newfile.txt', 'w') as newfile:
while True:
lines = [oldfile.readline() for _ in range(5)]
if not lines[0]:
break
if lines[1].rstrip() not in bad_words:
newfile.write( ''.join(lines) )
uj5u.com熱心網友回復:
- 由于資料末尾必須包含
million,您可以嘗試此代碼。
示例代碼:
bad_words = ['Paris',]
with open('DataSystem.txt') as oldfile, open('newfile.txt', 'w') as newfile:
lines = oldfile.readlines()
temp = []
is_bad = False
for line in lines:
temp.append(line)
for bad_word in bad_words:
if bad_word in line:
is_bad = True
break
if "million" in line:
if not is_bad:
for new_data in temp:
newfile.write(new_data)
is_bad = False
temp = []
結果:
LZQ
London
1572
11
8,982 million
Rome
1285
11
2,873 million
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/326424.html
