![](/img/trans.png)
[英]search a word in a file and print the line number where that word occurs in python
[英]get a word in a file and add the line number where that word occurs and add that number in list and add to dictionary
我正在嘗試編寫一個帶有一個參數,一個文件名(具有完整內容)並返回一個字典的函數索引,該字典中的鍵是文件中的單詞,值是包含唯一且以升序排列的行號的列表)這些字詞出現在其中。
為了獲得唯一的密鑰,我確實是這樣的:
with open(filename) as file:
text = file.read();
list1 = set(text.split())
print(list1)
for line_num, line in enumerate(file):
if any([word in line for word in list1]):
print (line_num, line)
無法獲得結果。
編輯:添加了示例數據:
LIET
Now, by Saint Peter's Church and Peter too,
He shall not make me there a joyful bride.
I wonder at this haste; that I must wed
Ere he, that should be husband, comes to woo.
I pray you, tell my lord and father, madam,
I will not marry yet; and, when I do, I swear,
It sh
答案應該是-
{'LIET': [1], 'Now,': [3], 'by': [3], 'Saint': [3], "Peter's": [3], 'Church': [3], 'and': [3, 7], 'Peter': [3], 'too,': [3], 'He': [4], 'shall': [4], 'not': [4, 8], 'make': [4], 'me': [4], 'there': [4], 'a': [4], 'joyful': [4], 'bride.': [4], 'I': [5, 7, 8], 'wonder': [5], 'at': [5], 'this': [5], 'haste;': [5], 'that': [5, 6], 'must': [5], 'wed': [5], 'Ere': [6], 'he,': [6], 'should': [6], 'be': [6], 'husband,': [6], 'comes': [6], 'to': [6], 'woo.': [6], 'pray': [7], 'you,': [7], 'tell': [7], 'my': [7], 'lord': [7], 'father,': [7], 'madam,': [7], 'will': [8], 'marry': [8], 'yet;': [8], 'and,': [8], 'when': [8], 'do,': [8], 'swear,': [8], 'It': [9], 'sh': [9]})
以下應該做的把戲
def index(filename):
word_lines = {}
with open(filename) as file:
for line_num, line in enumerate(file.readlines(), 1):
for word in line.split():
if word in word_lines.keys():
if line_num not in word_lines[word]:
word_lines[word].append(line_num)
else:
word_lines[word] = [ line_num ]
return word_lines
print(index('test.txt'))
該示例的輸出將是:
{'Now,': [1], 'by': [1], 'Saint': [1], "Peter's": [1], 'Church': [1], 'and': [1, 5], 'Peter': [1], 'too,': [1], 'He': [2], 'shall': [2], 'not': [2, 6], 'make': [2], 'me': [2], 'there': [2], 'a': [2], 'joyful': [2], 'bride.': [2], 'I': [3, 5, 6], 'wonder': [3], 'at': [3], 'this': [3], 'haste;': [3], 'that': [3, 4], 'must': [3], 'wed': [3], 'Ere': [4], 'he,': [4], 'should': [4], 'be': [4], 'husband,': [4], 'comes': [4], 'to': [4], 'woo.': [4], 'pray': [5], 'you,': [5], 'tell': [5], 'my': [5], 'lord': [5], 'father,': [5], 'madam,': [5], 'will': [6], 'marry': [6], 'yet;': [6], 'and,': [6], 'when': [6], 'do,': [6], 'swear,': [6], 'It': [7], 'sh': [7]}
我將假定文件內容已以字符串text
讀取。 我也刪除標點符號
text = "He shall not make me there a joyful bride.\n " \
"I wonder at this haste; that I must wed Ere he, that should be husband, comes to woo. \n" \
"I pray you, tell my lord and father, madam, I will not marry yet;"
punctuations = '!"#$%&\'()*+,-./:;<=>?@[\]^_`{|}~'
for punctuation in punctuations:
text = text.replace(punctuation, '')
words = list(set(text.split()))
print(words)
result_dict = {}
for line_num, line in enumerate(text.split('\n')):
for word in words:
if word in line:
if word in result_dict.keys():
result_dict[word].append(line_num)
else:
result_dict[word] = [line_num]
print(result_dict)
{
'a': [0, 1, 2],
'shall': [0],
'bride': [0],
'He': [0],
'make': [0],
'me': [0, 1],
'not': [0, 2],
'he': [0, 1, 2],
'there': [0],
'joyful': [0],
'husband': [1],
'and': [1, 2],
'must': [1],
'at': [1, 2],
'wonder': [1],
'to': [1],
'I': [1, 2],
'wed': [1],
'this': [1],
'Ere': [1],
'comes': [1],
'woo': [1],
'that': [1],
'haste': [1],
'be': [1],
'should': [1],
'father': [2],
'will': [2],
'pray': [2],
'my': [2],
'yet': [2],
'you': [2],
'tell': [2],
'lord': [2],
'marry': [2],
'madam': [2]
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.