Python:在文本文件中找到一个字符串,如果找到了,在下一行写一个字符串
我正在尝试编写一个简单的python代码,在其中查找文本文件中的字符串,一旦找到字符串,我想在txt文件的下一行中编写一些注释。我还想确保评论不会出现在下一行中。这是我到目前为止写的。然而,它并没有在下一行写注释,而是在另一个地方写(看起来我并没有对文件指针的当前位置使用正确的语法)。此外,即使我在下一行已有注释,代码的“else”部分也不会执行Python:在文本文件中找到一个字符串,如果找到了,在下一行写一个字符串,python,python-2.7,Python,Python 2.7,我正在尝试编写一个简单的python代码,在其中查找文本文件中的字符串,一旦找到字符串,我想在txt文件的下一行中编写一些注释。我还想确保评论不会出现在下一行中。这是我到目前为止写的。然而,它并没有在下一行写注释,而是在另一个地方写(看起来我并没有对文件指针的当前位置使用正确的语法)。此外,即使我在下一行已有注释,代码的“else”部分也不会执行 #!/comm/python/2.7.8/bin/python2.7 import re # Open a file fo = open("bt5a
#!/comm/python/2.7.8/bin/python2.7
import re
# Open a file
fo = open("bt5aura_bt5rf01_mixers.vams", "rw+")
print "Name of the file: ", fo.name
str1 = "// pragma coverage off"
# search for "module " to put "pragma coverge off" comment
for line in fo:
if 'module ' in line:
print line
nextLine=fo.next()
if nextLine.rstrip!=str1:
print "NO pragma comment found on next line:",nextLine.rstrip()
fo.seek(0,1)
line=fo.write(str1)
else:
print "Pragma off comment already present"
# Close opened file
fo.close()
修改代码以搜索两个字符串
#!/comm/python/2.7.8/bin/python2.7
import re
comment1 = "// pragma coverage off"
comment2 = "// pragma coverage on"
match1 = "module "
match2 = "assign Check "
# Open the file with ams filenames list.
with open('listofmodels.txt') as list_ams:
# Iterate over the lines, each line represents a file name.
for amsModel in list_ams:
content1 = []
content2 = []
amsModel = amsModel.rstrip('\n')
with open (amsModel, "r+b") as file:
print "***Processing amsModel=%s for pragma coverage off***" % amsModel
for line in file:
content1.append(line)
if match1 in line:
nextLine = file.next().rstrip()
if nextLine != comment1:
print "No pragma off comment found on next line,\n nextline=%s\n adding pragma off comment" % nextLine
content1.append(comment1 + "\n")
else:
print "Pragma off comment already present on next line"
content1.append(nextLine + "\n")
file.seek(0)
file.truncate()
file.write("".join(content1))
file.close
with open (amsModel, "r+b") as file:
print "***Processing amsModel=%s for pragma coverage on***" % amsModel
for line in file:
content2.append(line)
if match2 in line:
nextLine = file.next().rstrip()
if nextLine != comment2:
print "No pragma on comment found on next line,\n nextline=%s\n adding pragma on comment" % nextLine
content2.append(comment2 + "\n")
else:
print "Pragma on comment already present on next line"
content2.append(nextLine + "\n")
file.seek(0)
file.truncate()
file.write("".join(content2))
file.close
list_ams.close
请尝试以下代码。请注意,它会为文件中包含匹配项的每一行打印“Pragma off comment ready present on next line”
#!/comm/python/2.7.8/bin/python2.7
import re
comment = "// pragma coverage off"
match = "module"
content = []
with open ("bt5aura_bt5rf01_mixers.vams", "r+b") as file:
for line in file:
content.append(line)
if match in line:
nextLine = file.next().rstrip()
if nextLine != comment:
print "No pragma comment found on next line: %s" % nextLine
content.append(comment + "\n")
else:
print "Pragma off comment already present on next line"
content.append(nextLine + "\n")
file.seek(0)
file.truncate()
file.write("".join(content))
示例
文件包含以下文本
no mod
a module
no mod again
after the first run there should be a comment in the third row
this should not change if you run it again
运行该方法后,其外观如下所示:
no mod
a module
// pragma coverage off
no mod again
after the first run there should be a comment in the third row
this should not change if you run it again
…正如所料,即使您多次运行,也不会有超过一条评论:
no mod
a module
// pragma coverage off
no mod again
after the first run there should be a comment in the third row
this should not change if you run it again
编辑:回答您的其他问题
(在一行中搜索多个字符串并分别添加注释)
您可以使用字典将要添加的注释映射到特定匹配项。这会自动消除代码中的冗余
#!/comm/python/2.7.8/bin/python2.7
import re
comments = {"// pragma coverage off":"module ", "// pragma coverage on":"assign Check "}
content = []
# Open the file with ams filenames list.
with open('listofmodels.txt') as list_ams:
# Iterate over the lines, each line represents a file name.
for amsModel in list_ams:
amsModel = amsModel.rstrip('\n')
with open (amsModel, "r+b") as file:
for comment, match in comments.iteritems():
print "*** Processing amsModel = {0} for: {1} ***".format(amsModel, key)
for line in file:
content.append(line)
if value in line:
nextLine = file.next().rstrip()
if nextLine != comment:
print "No comment (\"{0}\") found on next line,\n nextline = {1}\n adding {0}".format(comment, nextLine, comment)
content.append(comment + "\n")
else:
print "comment (\"{0}\") already present on next line".format(comment)
content.append(nextLine + "\n")
file.seek(0)
file.truncate()
file.write("".join(content1))
file.close
最后一件事:代码中的缩进不正确。我不知道这是否是出于格式化目的,但请参阅以获取更多信息。请尝试以下代码。请注意,它会为文件中包含匹配项的每一行打印“Pragma off comment ready present on next line”
#!/comm/python/2.7.8/bin/python2.7
import re
comment = "// pragma coverage off"
match = "module"
content = []
with open ("bt5aura_bt5rf01_mixers.vams", "r+b") as file:
for line in file:
content.append(line)
if match in line:
nextLine = file.next().rstrip()
if nextLine != comment:
print "No pragma comment found on next line: %s" % nextLine
content.append(comment + "\n")
else:
print "Pragma off comment already present on next line"
content.append(nextLine + "\n")
file.seek(0)
file.truncate()
file.write("".join(content))
示例
文件包含以下文本
no mod
a module
no mod again
after the first run there should be a comment in the third row
this should not change if you run it again
运行该方法后,其外观如下所示:
no mod
a module
// pragma coverage off
no mod again
after the first run there should be a comment in the third row
this should not change if you run it again
…正如所料,即使您多次运行,也不会有超过一条评论:
no mod
a module
// pragma coverage off
no mod again
after the first run there should be a comment in the third row
this should not change if you run it again
编辑:回答您的其他问题
(在一行中搜索多个字符串并分别添加注释)
您可以使用字典将要添加的注释映射到特定匹配项。这会自动消除代码中的冗余
#!/comm/python/2.7.8/bin/python2.7
import re
comments = {"// pragma coverage off":"module ", "// pragma coverage on":"assign Check "}
content = []
# Open the file with ams filenames list.
with open('listofmodels.txt') as list_ams:
# Iterate over the lines, each line represents a file name.
for amsModel in list_ams:
amsModel = amsModel.rstrip('\n')
with open (amsModel, "r+b") as file:
for comment, match in comments.iteritems():
print "*** Processing amsModel = {0} for: {1} ***".format(amsModel, key)
for line in file:
content.append(line)
if value in line:
nextLine = file.next().rstrip()
if nextLine != comment:
print "No comment (\"{0}\") found on next line,\n nextline = {1}\n adding {0}".format(comment, nextLine, comment)
content.append(comment + "\n")
else:
print "comment (\"{0}\") already present on next line".format(comment)
content.append(nextLine + "\n")
file.seek(0)
file.truncate()
file.write("".join(content1))
file.close
最后一件事:代码中的缩进不正确。我不知道这是否是出于格式化目的,但请参阅以了解更多信息。:非常感谢。这很有效。现在我试图修改这段代码,以查找两个字符串,并在匹配时添加注释。我添加了中断循环,因为我只想在第一场比赛中实现它。然而,修改后的代码给了我一个空白文件作为输出。我假设这是由于错误的文件处理操作造成的。另外,我实际上希望在找到第一个字符串之后继续下一个字符串搜索。在这种情况下,是否只设置文件指针file.seek(1)?若并没有找到第一个字符串,那个么我不希望继续找到第二个字符串。实际上我已经找到了。还修改了代码,使其将文件作为输入,其中包含文件路径列表。它逐个打开所有文件并执行“双字符串搜索”操作。用新代码更新了原始帖子。如果你还有什么建议,请告诉我。再次感谢你的帮助。很高兴我能帮忙!我用改进版的双字符串搜索更新了我的答案。如果此答案解决了您的问题,请单击答案旁边的复选标记,将其标记为已接受。:非常感谢。这很有效。现在我试图修改这段代码,以查找两个字符串,并在匹配时添加注释。我添加了中断循环,因为我只想在第一场比赛中实现它。然而,修改后的代码给了我一个空白文件作为输出。我假设这是由于错误的文件处理操作造成的。另外,我实际上希望在找到第一个字符串之后继续下一个字符串搜索。在这种情况下,是否只设置文件指针file.seek(1)?若并没有找到第一个字符串,那个么我不希望继续找到第二个字符串。实际上我已经找到了。还修改了代码,使其将文件作为输入,其中包含文件路径列表。它逐个打开所有文件并执行“双字符串搜索”操作。用新代码更新了原始帖子。如果你还有什么建议,请告诉我。再次感谢你的帮助。很高兴我能帮忙!我用改进版的双字符串搜索更新了我的答案。如果此答案解决了您的问题,请单击答案旁边的复选标记将其标记为已接受。