用python实现文件比较
来源:互联网 发布:windows 流媒体服务器 编辑:程序博客网 时间:2024/06/04 19:30
越来越发现python非常适合做一些日常开发的工具。
平时,我们经常用一些比较工具,比较目录、比较两个文本文件的变化。最近发现,python的标准库里居然带了这些功能的算法。自己处理一下,就可以写出一个很实用的比较工具了。
文件和目录比较Module叫做filecmp。最酷的是他提供了一个叫dircmp的类,可以直接比较两个目录,给出下面的结果。
- left_list
- Files and subdirectories in a, filtered by hide and ignore.
right_list
- Files and subdirectories in b, filtered by hide andignore.
common
- Files and subdirectories in both a and b.
left_only
- Files and subdirectories only in a.
right_only
- Files and subdirectories only in b.
common_dirs
- Subdirectories in both a and b.
common_files
- Files in both a and b
common_funny
- Names in both a and b, such that the type differs between the directories, or names for whichos.stat() reports an error.
same_files
- Files which are identical in both a and b.
diff_files
- Files which are in both a and b, whose contents differ.
funny_files
- Files which are in both a and b, but could not be compared.
subdirs
- A dictionary mapping names in common_dirs to dircmp objects.
另一个重要的模块是使difflib。包括两个类SequenceMatcher,使这个模块的基础,可以用来比较任意两个序列的变化,list/string都没问题。另外一个Differ,可以比较两个文本文件的差别,产生一个文本文件的报告。还有一个更酷的HTMLDiff,可以直接产生HTML格式的比较报告。感觉后两个类的主要价值是提供了一个框架,我们可以做一些修改,按照自己的格式定制报告。如果对报告要求特别,可以直接使用SequenceMatcher。
贴个小例子:[这个代码是pyhton 2.x的]
def reportSingleFile(srcfile, basefile, rpt):
src = file(srcfile).read().split('')
base = file(basefile).read().split('')
import difflib
s = difflib.SequenceMatcher( lambda x: len(x.strip()) == 0, # ignore blank lines
base, src)
lstres = []
for tag, i1, i2, j1, j2 in s.get_opcodes():
print (tag, i1, i2, j1, j2)
#print lstres
if tag == 'equal':
pass
elif tag == 'delete' :
lstres.append('DELETE (line: %d)' % i1)
lstres += base[i1:i2]
lstres.append('')
elif tag == 'insert' :
lstres.append('INSERT (line: %d)' % j1)
lstres += src[j1:j2]
lstres.append('')
elif tag == 'replace' :
lstres.append('REPLACE:')
lstres.append('Before (line: %d) ' % i1)
lstres += base[i1:i2]
lstres.append('After (line: %d) ' % j1)
lstres += src[j1:j2]
lstres.append('')
else:
pass
print ''.join(lstres)
src = file(srcfile).read().split('')
base = file(basefile).read().split('')
import difflib
s = difflib.SequenceMatcher( lambda x: len(x.strip()) == 0, # ignore blank lines
base, src)
lstres = []
for tag, i1, i2, j1, j2 in s.get_opcodes():
print (tag, i1, i2, j1, j2)
#print lstres
if tag == 'equal':
pass
elif tag == 'delete' :
lstres.append('DELETE (line: %d)' % i1)
lstres += base[i1:i2]
lstres.append('')
elif tag == 'insert' :
lstres.append('INSERT (line: %d)' % j1)
lstres += src[j1:j2]
lstres.append('')
elif tag == 'replace' :
lstres.append('REPLACE:')
lstres.append('Before (line: %d) ' % i1)
lstres += base[i1:i2]
lstres.append('After (line: %d) ' % j1)
lstres += src[j1:j2]
lstres.append('')
else:
pass
print ''.join(lstres)
用久了C++,算法要么自己写,要么去网上找。用Python之后,感觉真的不一样了。虽然python的运行效率不高,但是开发效率确实非常高。很适合做一些平时用的小工具。
- 用python实现文件比较
- python实现文件比较,匹配域名
- Python 实现比较文件内容异同
- 通过python利用哈希值实现比较两个文件的一致性
- python文件比较的lib
- Python模块filecmp 文件比较
- 版本号比较的python实现
- python实现时间的比较
- java 实现文件简单比较
- 用标准模板库STL实现文件比较
- 用标准模板库STL实现文件比较
- 用python 实现上传文件功能
- python实现用socket传输文件
- 用python实现自动删除svn文件
- 用Python实现Excel导出Json文件
- 用python-os模块实现文件查找
- python实现用socket传输文件
- 用python代码实现文件的拷贝
- SiteMaps_Wicked Code:您一直期待的 SQL 站点映射提供程序
- java 面试大全
- 心有些慌
- DEBUG和RELEASE 版本差异及调试相关问题:
- 数据库系统崩溃后的一般处理步骤(引自大牛邹建)
- 用python实现文件比较
- WAS OOM(out of memery)挂死的常规分析
- LuaTinker向Linux移植成功
- jsp与severlet
- DataGrid中的高级ToolTip
- Debug和Release之区别
- 改变.H,.CPP文件图标为原来的显示方式
- TCP连接数过大导致down机的原因分析
- 站长资讯网开始上线,欢迎光临 http://www.adminfan.com