根据用户输入在文件中查找一行

时间:2014-06-25 17:24:54

标签: python file-io python-2.x

目前这个程序找到了你要求它找到的具有特定单词的所有行,但是我如何更改它以便将所有多个行号存储为不同的值,然后我可以操作它们。或者如何找到短语的具体出现次数。

a = raw_input("Please enter something: ")
lookup = a
with open('FileName') as myFile:
    for num, line in enumerate(myFile, 1):
        if lookup in line:
            print 'found at line:', num

3 个答案:

答案 0 :(得分:1)

您可以将其存储到列表中(或同时执行这两项操作),而不是打印num

found = []
for num, line in enumerate(myFile, 1):
    if lookup in line:
        print 'found at line:', num
        found.append(num)
        # found.append((line,num)) # use this if you want to store both the line and the number

这个的简短版本是:

found = [num for num, line in enumerate(myFile, 1) if lookup in line]

打印num的简短版本:

found = [num for num, line in enumerate(myFile, 1) if lookup in line and not print(num)]

这将为您提供一个列表found,其中存储了您的查找成功的所有nums

答案 1 :(得分:0)

lookup = raw_input("Please enter something: ")
with open("FileName") as myfile:
    found = {num: line for (num, line) in enumerate(myfile, 1)}

我就是这样做的。 dict comp有点简洁,但拼写出来的确如此:

found = {} # empty dict
for num, line in enumerate(myfile, 1):
    found[num] = line

这将让您像这样使用字典:

for lineno, line in found.items():
    print "{:03}: {}".format(lineno, line)

在评论中编辑您的问题

我个人使用正则表达式,而不是试图将它们混合在一起。但是要回答你的问题:

startsentry="DATA STARTS ON THE NEXT LINE"
endsentry="DATA ENDS ON THE PREVIOUS LINE"

with open('path/to/file') as myfile:
    for line_num, line in enumerate(myfile, 1):
        if startsentry in line:
            _start = line_num+1
        if endsentry in line:
            _end = line_num
    data_is_in = (_start, _end)

答案 2 :(得分:0)

我建议采用不同的方法,例如:

lines = []
with open("file") as f:
  lines = f.readlines()

phrases = filter( lambda x: "foo" in x[1], enumerate(lines) )
print phrases

现在剩下一系列与“foo”匹配的行号和短语对。

更新:更改为每条评论的枚举(谢谢!)。