difflib 如何把比较结果输出到文件

syhhl123 2013-07-18 01:16:32
#-*- coding:utf-8 -*-
import os,difflib
from difflib import *

if __name__=='__main__':

d1=open(r"d:\py\1.py").read().splitlines()
d2=open(r"d:\py2\1.py").read().splitlines()
diff=difflib.unified_diff(d1,d2,lineterm='')
print '\n'.join(list(diff))
newb=open(r'logoutput.txt','w')
newb.write(diff) #这么写不行,应该怎么写呢?
newb.close()
...全文
649 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
wangyangkobe 2013-07-20
  • 打赏
  • 举报
回复
引用 10 楼 wangyangkobe 的回复:
#!/bin/python

import difflib

def main():
    text1 = """http://www.vpsee.com is a website which is dedicated for 
building scalable websites on cloud platforms. The keywords are: Linux, Mac,
Cloud Computing, C, Python, MySQL, Nginx, VPS, Performance, Scalability,
Architecture, ..., etc. Have fun!"""
    text1_lines = text1.splitlines()

    text2 = """http://VPSee.com is a website which is dedicated for 
building scalable websites on cloud platforms. The keywords are: Linux, Mac,
Cloud Computing, C, Python, MySQL, Nginx, VPS, Performance, Scalability,
Programming, Optimisation, Architecture, ... , etc. Have fun !"""
    text2_lines = text2.splitlines()

    d = difflib.HtmlDiff()
    with open("result.html", "w") as f:
        for line in d.make_file(text1_lines, text2_lines):
            f.write(line)


if __name__ == '__main__':
    main()
可以使用make_file 或者 make_table
wangyangkobe 2013-07-20
  • 打赏
  • 举报
回复
#!/bin/python

import difflib

def main():
    text1 = """http://www.vpsee.com is a website which is dedicated for 
building scalable websites on cloud platforms. The keywords are: Linux, Mac,
Cloud Computing, C, Python, MySQL, Nginx, VPS, Performance, Scalability,
Architecture, ..., etc. Have fun!"""
    text1_lines = text1.splitlines()

    text2 = """http://VPSee.com is a website which is dedicated for 
building scalable websites on cloud platforms. The keywords are: Linux, Mac,
Cloud Computing, C, Python, MySQL, Nginx, VPS, Performance, Scalability,
Programming, Optimisation, Architecture, ... , etc. Have fun !"""
    text2_lines = text2.splitlines()

    d = difflib.HtmlDiff()
    with open("result.html", "w") as f:
        for line in d.make_file(text1_lines, text2_lines):
            f.write(line)


if __name__ == '__main__':
    main()
SFinx 2013-07-18
  • 打赏
  • 举报
回复
引用 7 楼 syhhl123 的回复:
是这样吗?但是输出还是空的。麻烦再指点下。 print '\n'.join(list(diff)) newbook=open(r'logoutput.txt','w') ss = list(diff) newn.write('\n'.join(ss)) newb.close()
diff是generator,你是不是在上面的运行结果下面直接跟着运行了? 重新
diff=difflib.unified_diff(d1,d2,lineterm='')
ImN1 2013-07-18
  • 打赏
  • 举报
回复
看#4 然后理解一下汉语里面“二次”“各自”是什么意思 很晕
syhhl123 2013-07-18
  • 打赏
  • 举报
回复
是这样吗?但是输出还是空的。麻烦再指点下。 print '\n'.join(list(diff)) newbook=open(r'logoutput.txt','w') ss = list(diff) newn.write('\n'.join(ss)) newb.close()
ImN1 2013-07-18
  • 打赏
  • 举报
回复
唉,点不通…… print '\n'.join(list(diff)) 你这样写就意味着你明白diff不是个字符串,就算list后也不是字符串,怎呢直接write呢? newb.write('\n'.join(ss))
syhhl123 2013-07-18
  • 打赏
  • 举报
回复
是这样吗?但是输出是空的。还是不行。 ss = list(diff) newb.write(ss)
ImN1 2013-07-18
  • 打赏
  • 举报
回复
generator不能二次使用 你要先list赋值给一个变量,然后print/write各自调用这个变量
syhhl123 2013-07-18
  • 打赏
  • 举报
回复
newb.write(str(diff))这种也不行,我试过了。 写进去是下面这种东西。 <generator object unified_diff at 0x013FAF80> print '\n'.join(list(diff))改成write也不行,什么都没有。
chenhaiou553166961 2013-07-18
  • 打赏
  • 举报
回复
newb.write(str(diff))#只能写str类型的
ImN1 2013-07-18
  • 打赏
  • 举报
回复
你都懂print '\n'.join(list(diff)) 不晓得把它改为 write 么?

37,721

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧