Python 搜索文本文件并打印行号
如何让函数在文本文件中查找单词所在的行并打印相应的行号 我必须打开一个包含段落的文本文件,然后在段落中搜索某些单词,然后打印单词的具体行号 这是我到目前为止所拥有的Python 搜索文本文件并打印行号,python,file,Python,File,如何让函数在文本文件中查找单词所在的行并打印相应的行号 我必须打开一个包含段落的文本文件,然后在段落中搜索某些单词,然后打印单词的具体行号 这是我到目前为止所拥有的 words = [network, devices, computer, fire, local, area, room, single] def index(string): lines = open('Network.txt', 'r') string = str(lines.read())
words = [network, devices, computer, fire, local, area, room, single]
def index(string):
lines = open('Network.txt', 'r')
string = str(lines.read())
lines.close()
return string
假设您已正确打开文件,这实际上相当容易。使用file.read()将不需要的整个文件拉入。因为它使文件的打开、关闭和错误处理变得更加容易:
with open(filename) as file:
for line in file:
#do something
逻辑的核心部分是,它接受一个iterable并返回一个计数以及每个迭代项
words = ["word","another"]
for line_num,line in enumerate(file):
if any([word in line for word in words]):
print line_num, line
另一个因素是列表理解,它检查单词是否在一行上。函数“如果iterable的任何元素为True,则返回True”。以及以下列表:
[word in line for word in words]
可以理解为:
[
告诉我word
是否在所有单词的行中
如果该数组中有任何单词,即至少有一个单词是该行,则该行为true,因此将被打印。假设您已正确打开文件,这实际上相当容易。使用file.read()将不需要的整个文件拉入。因为它使文件的打开、关闭和错误处理变得更加容易:
with open(filename) as file:
for line in file:
#do something
逻辑的核心部分是,它接受一个iterable并返回一个计数以及每个迭代项
words = ["word","another"]
for line_num,line in enumerate(file):
if any([word in line for word in words]):
print line_num, line
另一个因素是列表理解,它检查单词是否在一行上。函数“如果iterable的任何元素为True,则返回True”。以及以下列表:
[word in line for word in words]
可以理解为:
[
告诉我word
是否在所有单词的行中
如果该数组中有任何
单词,即至少有一个单词为该行,则该行为真,因此将被打印出来。尝试以下操作:
words = []
lines = {}
for i in words:
lines[i] = []
with open("file", "r") as fin:
curLine = 0
for i in fin.readLines():
for j in words:
if j in i:
lines[j].append(curLine)
curLine += 1
for i in words:
print lines[j]
试试这个:
words = []
lines = {}
for i in words:
lines[i] = []
with open("file", "r") as fin:
curLine = 0
for i in fin.readLines():
for j in words:
if j in i:
lines[j].append(curLine)
curLine += 1
for i in words:
print lines[j]
如果您只想测试是否存在单个单词,请使用所述行的a和a:
words={'some', 'target', 'words', 'in', 'a', 'set'}
with open(f_name) as fin:
for line_num, line in enuemrate(fin):
if set(line.split()) & words:
print(line_num, line)
如果您只想测试是否存在单个单词,请使用所述行的a和a:
words={'some', 'target', 'words', 'in', 'a', 'set'}
with open(f_name) as fin:
for line_num, line in enuemrate(fin):
if set(line.split()) & words:
print(line_num, line)
这些代码都不会搜索文件。我知道它会打开文件。这就是为什么我要问!使用lines=lines.Split('\n')
按新行拆分行。返回一个列表,每行作为一个元素。这样就很容易找出哪一行有您要查找的单词。@Ryan最好使用for line in file
对文件进行迭代,将文件视为一个iterable将自动拆分为新行(任何类型)。这些代码都不会搜索该文件。我知道它会打开该文件。这就是为什么我要问!使用lines=lines.Split('\n')
按新行拆分行。返回一个列表,每行作为一个元素。这样就很容易找出哪一行有你要查找的单词。@Ryan最好使用for line in file
遍历文件,将文件视为一个iterable将自动在新行(任何类型)上拆分。可能1)any(word in line for words in words)
和words={“word”,“other”}
(即,在any
中使用生成器与列表理解,并使用集合而不是列表作为测试单词的目标。我使用OP为单词提供的数据结构,并且考虑到它有多短,我只使用了简单的列表理解,因为使用了一个函数,该函数看起来像中的指令。)混乱。any(stuff…
vsany([stuff…])
可能1)any(单词中的单词对应单词)
和words={“word”,“other”}
(即,在any
中使用生成器与列表理解,并使用集合而不是列表作为测试单词的目标。我使用OP为单词提供的数据结构,并且考虑到它有多短,我只使用了简单的列表理解,因为使用了一个函数,该函数看起来像中的指令。)混乱。any(stuff…
vsany([stuff…])