我正在尝试编写一个简单的python代码,我在文本文件中查找字符串,一旦找到字符串,我想在txt文件的下一行写一些注释。我还想确保评论在下一行中不存在。这是我到目前为止所写的内容。然而,它不是在下一行写注释而是在不同的地方写(看起来我没有使用正确的语法来获取文件指针的当前位置)。即使我的下一行已有评论,代码中的“else”部分也永远不会执行。
#!/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
答案 0 :(得分:2)
尝试以下代码。请注意,它会为包含"Pragma off comment already present on next line"
的文件中的每一行打印match
。
#!/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
最后一件事:代码中的缩进不正确。我不知道是否是由于SO格式化目的,但请参阅here以获取更多信息。