Python循环文本文件以查找和打印
此Python代码运行但打印文本文件的最后一页。不知道为什么,但我的目标是打印指定文本行(包含特定字符串**Direct**的行)下面的整行文本。如何在文本文件中循环,在每一行中搜索指定的字符串,并在找到该字符串时直接在其下方打印该行?我搜索了很多在线论坛,没有找到一个容易理解的例子。我使用PythonSypder2.7。谢谢你的帮助Python循环文本文件以查找和打印,python,python-2.7,Python,Python 2.7,此Python代码运行但打印文本文件的最后一页。不知道为什么,但我的目标是打印指定文本行(包含特定字符串**Direct**的行)下面的整行文本。如何在文本文件中循环,在每一行中搜索指定的字符串,并在找到该字符串时直接在其下方打印该行?我搜索了很多在线论坛,没有找到一个容易理解的例子。我使用PythonSypder2.7。谢谢你的帮助 import os Path = '..\\Test.txt' if os.path.isfile(Path): with open(Path) as
import os
Path = '..\\Test.txt'
if os.path.isfile(Path):
with open(Path) as f:
for line in f:
print line
else:
print Path, "doesn't exist"
f.close()
查看模块。它包括re.search()
函数,用于搜索字符串中的模式
要打印下一行,可以使用f.next()
,利用文件对象是可编辑的这一事实
例如,您可以执行以下操作:
import os
import re
Path = 'foo.txt'
Pattern = "spam" # String to be searched
if os.path.isfile(Path):
with open(Path) as f:
for line in f:
if re.search(Pattern, line):
print(f.next())
else:
print(Path, "doesn't exist")
顺便说一下,您不需要最后的
f.close()
。它已经由with
语句处理。检查文件中的任何一行中是否有一行
,找到的下一行的打印将是下一行。没有文件将设置为currentlineno+1
临时文件的内容
one
two
three
four
Python文件
with open('temp') as f:
found=-1
#no lines are found to print so found is negative in value since line no in a file cannot be negative
for index,line in enumerate(f):
if found==index:
print(line)
if 'one' in line:
found=index+1
# when the condition is True found is set to the line no that we need to print
输出
two
Python3.x:
dummyFile= "dummy.txt"
searchString = 'Anday Wala Burger'
with open(dummyFile) as f:
content = f.readlines()
# you may also want to remove empty lines
content = [l.strip() for l in content if l.strip()]
# flag
nextLine = False
for line in content:
if searchString in line:
nextLine = not nextLine
else:
if nextLine:
print(line)
nextLine = not nextLine
else:
pass
dummy.txt
Mango
Anday Wala Burger
40
Aloo
Anday
Ghobi
Anday Wala Burger
30
Kheerey
Anday Wala Burger
py:
输出:
40
30
40
30
编辑:
Python 2.7:
dummyFile= "dummy.txt"
searchString = 'Anday Wala Burger'
with open(dummyFile) as f:
content = f.readlines()
# you may also want to remove empty lines
content = [l.strip() for l in content if l.strip()]
# flag
nextLine = False
for line in content:
if searchString in line:
nextLine = not nextLine
else:
if nextLine:
print(line)
nextLine = not nextLine
else:
pass
输出:
40
30
40
30
您需要进行一些更改: 1.读台词 2.-与你的文本进行比较
import os
Path = '..\\Test.txt'
look='**Direct**'
if os.path.isfile(Path):
with open(Path, "r") as f:
for line in f:
if look in line:
print (line)
else:
print (Path, "doesn't exist")
您搜索的字符串是什么?请检查此解决方案是否有帮助!-字符串是指定字符串正下方的一行。该字符串是一个数字,并且该数字会随着文本文件的进行而变化,例如,第一个实例可能是20,下一个30,下一个40等等,但每个实例都会在指定的string@Nui好吧,这听起来和下面的答案完全不同。要搜索的
数字字符串
下面的指定的字符串是什么?@Nui我已根据您在评论中的要求编辑了我的答案!谢谢你。这是可行的,但实际上我需要在指定的look文本的正下方画一行。我认为@Albin Paul将其与'index+1'结合使用,效果很好,Paul!Paul why is found=-1首次定义?索引是内置的Python术语吗(因此无需预先定义)?@Nui之所以定义它,是因为我检查文件的索引或行号是否等于found,所以必须定义found以进行检查,否则它将显示赋值前引用的错误。Python中的索引似乎是通用的,从0开始。与enumerate一起使用时,“索引”是enumerate的“值”部分,对吗?您的代码在没有found=-1@Albin Paul的情况下以相同的方式运行。请在相应的if行中为“found=-1”和“found行”添加一些注释/注释,注释不用于扩展讨论;这段对话已经结束。