我目前正在嘗試掃描文本,識別單詞多次出現的行并將這些行保存在串列中。如果該詞沒有出現在文本中,則應回傳一個空串列。
這就是我到目前為止所擁有的;
def line_number(text,word):
with open(text) as file:
lines = file.readlines()
for line_number, line in enumerate(lines,1):
if word in line:
print(f'{word} is in the line {line_number}')
else:
pass
print("None")
此時,我可以列印出出現單詞的行,但我需要一種方法來保存這些行
uj5u.com熱心網友回復:
似乎是基于 SEO 的功能。也許你可以做的是在 print 陳述句之后保存這些行,也許寫那行并在單獨的檔案中關閉,因為你可能需要它,并分配一個觸發器來作業它,其中每一行構成第一行的地址。
uj5u.com熱心網友回復:
tuple (line, count)您可以為每個linein附加一個lines:
def line_number(text, word):
with open(text) as file:
lines = file.readlines()
lst = [(x.strip(), x.count(word)) for x in lines if x.strip()]
return lst
一個測驗檔案,包含word='test':
test test test
test sdf sdfuih test
asdlkj
123
回傳
[('test test test', 3),
('test sdf sdfuih test', 2),
('asdlkj', 0),
('123', 0)]
然后,您可以使用以下方法對您的事件進行排序或最大化:
>>> x = line_number("file2.txt", "test")
# Sorted
>>> sorted(x, key=lambda a: a[0], reverse=True)
[('test test test', 3),
('test sdf sdfuih test', 2),
('asdlkj', 0),
('123', 0),
# Max
>>> max(x, key=lambda a: a[0])
('test test test', 3)
或者您可以保存行號,而不是line
# i 1 means first line is 1
lst = [(i 1, x.count(word)) for i, x in enumerate(lines) if x.strip()]
編輯:根據您的評論要求,這只是獲取出現的行號word:
# returns [1, 3] for test file above
lst = [i 1 for i, x in enumerate(lines) if word in x]
uj5u.com熱心網友回復:
使用enumerate,您只需要創建一個空串列并附加它。
def line_number(text, word):
my_list = []
with open(text) as f:
lines = f.readlines()
for id, line in enumerate(lines):
if word in line:
#print(f"{word} --> {id}")
my_list.append(id)
return(my_list)
text = "tes_test_text.txt"
word = "stack"
print(line_number(text, word))
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/477293.html
上一篇:使用BeautifulSoup從HTML字串中獲取文本和影像url
下一篇:僅在按下按鈕時使用克隆助手
