zoukankan      html  css  js  c++  java
  • 精悍的Python代码段

    1 文件处理

    readlines() 和 writelines()方法可以讲列表元素依次写到文件中;

    file类本身没有提供复制方法,可以使用read()和write()方法模拟实现文件的拷贝,也可以使用shutil模块:

    shutil.copyfile('hello.txt','hello2.txt')
    shutil.move('hello.txt','../')
    shutil.copyfile('hello2.txt','hello3.txt')

    批量改变当前目录中文件的名字:

    方式一:

    files = os.listdir(".")
    for filename in files:
        pos = filename.find(".");
        if filename[pos+1:] == 'html':
            newname = filename[:pos+1] + 'htm'
            os.rename(filename,newname)

    方式二:

    files = os.listdir(".")
    for filename in files:
        li = os.path.splitext(filename)
        if li[1] == 'html':
            newname = li[0] + '.htm'
            os.rename(filename,newname)

    方式三:

    可以使用glob模块中的glob方法获取指定条件的文件列表:

    例如使用l = glob.glob('*.html')获取html为后缀的文件

    l = glob.glob('c:\\w*\\*.txt')获取C盘中w开头目录中所有的文本文件。

    统计文件中指定字符串的数量(是统计字符串而不是单词,字符串可以不是整个单词):

    import re
    def str_count(filename,s):
        count = 0
        f = file(filename,'r+')
        for line in f.readlines():
            li = re.findall(s,line);
            if li.count(s) > 0:
                count += li.count(s);
        f.close()
        return count

    统计文件中指定单词的数量:

    def word_count(filename,word):
        count = 0
        f = file(filename,'r+')
        for line in f.readlines():
            p = r'\b'+word+r'\b'
            print p
            li = re.findall(p,line);
            print li
            if li.count(word) > 0:
                count += li.count(word);
        f.close()
        return count

    修改文件中指定字符串的代码,这里面有两种修改方式,即是否进行全词匹配:

    #f1源文件,f2修改后文件,s待修改的字符串,_s为改后字符串,whole是否全词匹配,默认值0代表全词匹配
    def replace_str (file1,file2,s,_s,whole = 0):
        f1 = file(file1,'r+')
        f2 = file(file2,'w+')
        p = r'\b' + s + r'\b'
        if whole == 1:
            p = s
        for line in f1.readlines():   
            t = re.sub(p,_s,line)
            f2.write(t)
        f1.close()
        f2.close()

    difflib是python提供的比较序列(string list)差异的模块。实现了三个类:

    1>SequenceMatcher 任意类型序列的比较 (可以比较字符串)
    2>Differ 对字符串进行比较
    3>HtmlDiff 将比较结果输出为html格式.


    使用difflib进行序列的比较与结果输出:

    import difflib
    from pprint import pprint
    a = 'pythonclub.org is wonderful'
    b = 'Pythonclub.org also wonderful'
    s = difflib.SequenceMatcher(None, a, b)
    print "s.get_matching_blocks():"
    pprint(s.get_matching_blocks())
    print
    print "s.get_opcodes():"
    for tag, i1, i2, j1, j2 in s.get_opcodes():
        print ("%7s a[%d:%d] (%s) b[%d:%d] (%s)" %  (tag, i1, i2, a[i1:i2], j1, j2, b[j1:j2]))

    来看另一个例子:

    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)

    使用Differ进行字符串比较

    import difflib
    diff = difflib.Differ().compare("start up","starT u4p")    
    print "\n".join(list(diff))

    使用HtmlDiff进行统计:

    from difflib import *
    s = HtmlDiff.make_file(HtmlDiff(),"start up","storT up")
    f=open(r"C:/dong/result.html","w")
    f.write(s)
    f.close()

    越来越发现python非常适合做一些日常开发的工具。平时,我们经常用一些比较工具,比较目录、比较两个文本文件的变化。最近发现,python的标准库里居然带了这些功能的算法。自己处理一下,就可以写出一个很实用的比较工具了。文件和目录比较Module叫做filecmp。最酷的是他提供了一个叫dircmp的类,可以直接比较两个目录。

    目录遍历的三种方式:

    1 使用递归函数:
    def visitDir (path):
        li = os.listdir(path)
        for p in li:
            pathname = os.path.join(path,p)
            if not os.path.isfile(pathname):
                visitDir(pathname)
            else:
                print pathname

    2 使用os.path.walk()

    import os,os.path

    def visitDir (arg,dirname,names):
        for filepath in names:
            print os.path.join(dirname,filepath)

    path1 = 'C:\\dong'     
    os.path.walk(path1,visitDir,())

    3 使用os.walk()

    def visitDir (path):
        for root,dirs,files in os.walk(path):
            for filepath in files:
                print os.path.join(root,filepath)

    path1 = 'C:\\dong'     
    visitDir(path1)

    自己制作的简单日志记录文件:

    #coding=gbk
    import sys,time

    sys.stderr = open("record.log","a")
    f = open(r"hello.txt",'r')
    t = time.strftime("%Y-%m_%d%X",time.localtime())
    context = f.read()
    if context:  #不为空
        sys.stderr.write(t + " " + context)
    else:
        raise Exception,t+"异常信息"

    使用Python模拟Java中的输入和输出流:

    #coding=gbk
    def FileInputStream (filename):
        try:
            f = open(filename)
            for line in f:
                for byte in line:
                    yield byte
        except StopIteration,e:
            f.close()
            return
       
    def FileOutputStream (inputStream,filename):
        try:
            f = open(filename,'w')
            while True:
                byte = inputStream.next()
                f.write(byte)
        except StopIteration,e:
            f.close()
            return

  • 相关阅读:
    POJ 1659 Frogs' Neighborhood
    zoj 2913 Bus Pass(BFS)
    ZOJ 1008 Gnome Tetravex(DFS)
    POJ 1562 Oil Deposits (DFS)
    zoj 2165 Red and Black (DFs)poj 1979
    hdu 3954 Level up
    sgu 249 Matrix
    hdu 4417 Super Mario
    SPOJ (BNUOJ) LCM Sum
    hdu 2665 Kth number 划分树
  • 原文地址:https://www.cnblogs.com/guojidong/p/2830009.html
Copyright © 2011-2022 走看看