比较txt文档与python之间的行
我脚本的这一部分旨在将网站页面与上次运行的文本文档进行比较。 我遇到的问题是,它只返回一行已更改的内容,并且忽略了之后的所有内容 我需要做什么改变才能使所有的东西都能移动比较txt文档与python之间的行,python,Python,我脚本的这一部分旨在将网站页面与上次运行的文本文档进行比较。 我遇到的问题是,它只返回一行已更改的内容,并且忽略了之后的所有内容 我需要做什么改变才能使所有的东西都能移动 def moo(DocIn, DocOut, WebAddy, Pub, Pub2, PDF): os.rename(DocIn, DocOut) # Renames the old compare document to 2 file(DocIn, "wb").write(urllib2.urlopen(W
def moo(DocIn, DocOut, WebAddy, Pub, Pub2, PDF):
os.rename(DocIn, DocOut) # Renames the old compare document to 2
file(DocIn, "wb").write(urllib2.urlopen(WebAddy).read()) # Creates a new document to compare from
hosts0 = open(DocIn,"rb")
hosts1 = open(DocOut,"rb")
lines1 = hosts0.readlines()
for i, lines2 in enumerate(hosts1):
if lines2 != lines1[i]:
chgdoc(DocOut, lines2)
print "line ", i, " in hosts1 is different \n"
print lines2
def chgdoc(DocOut, lines2):
fileName, fileExtension = os.path.splitext(DocOut)
file = open(fileName + '_3' + fileExtension, "a")
file.write(lines2)
file.close()
创建已更改内容的新文档。对正在更改的内容进行迭代是个坏主意。定义线条2,就像定义线条1一样。这可能就够了。您的chgdoc方法是第一次创建一个新文档,然后在其中添加行吗?我认为您可能正在覆盖循环中的文档。如果你能告诉我们chgdoc是什么样子会更好。另一件事是在处理文件时使用Python的with运算符?此外,尝试打印您正在迭代的内容,以查看循环中发生了什么。