zoukankan      html  css  js  c++  java
  • python文件读取,替换(带格式,python lib 库)

    import os, time
    import sys
    import re
    
    
    def read_old_part(filename, start, end):
        content = []
        recording = False
        with open(filename) as f:
            for line in f:
                line = line.strip()
                if line == end:
                    break
                if recording:
                    content.append(line)
                if line == start:
                    recording = True
        #    return '
    '.join(content)
        return content
    
    
    def read_all_part(filename):
        with open(filename) as f:
            lines = f.readlines()
        return lines
    
    
    def read_all_part_strip(filename):
        with open(filename) as f:
            all_part = []
            for line in f:
                line = line.strip()
                all_part.append(line)
        return all_part
    
    filename = "test.log"
    start = 'def find_vcvarsall(version):'
    end = 'def query_vcvarsall(version, arch="x86"):'
    add_str1 = '''    vcvarsall = r"C:UsersyuxinglxAppDataLocalProgramsCommonMicrosoftVisual C++ for Python9.0vcvarsall.bat"
    '''
    add_str2 = "    return vcvarsall
    
    "
    old_str = read_old_part(filename, start, end)
    all_str = read_all_part(filename)
    add_str_strip = read_all_part_strip(filename)
    old_str_nu = len(old_str)
    all_str_nu = len(all_str)
    all_str_strip_nu = len(add_str_strip)
    
    if all_str_nu == all_str_strip_nu:
        for line in old_str:
            if line in add_str_strip:
                all_str_strip_nu = add_str_strip.index(old_str[0])
    
        del all_str[all_str_strip_nu:(old_str_nu + all_str_strip_nu)]
        all_str_nu = add_str_strip.index(start)
        all_str.insert(all_str_nu + 1, add_str1)
        all_str.insert(all_str_nu + 2, add_str2)
        f = open('test.txt', 'w')
        f.writelines(all_str)
        f.close()
    else:
        print "it's worng"
    

     test.log

            if i not in newList:
                newList.append(i)
        newVariable = os.pathsep.join(newList)
        return newVariable
    
    def find_vcvarsall(version):
        """Find the vcvarsall.bat file
    
        At first it tries to find the productdir of VS 2008 in the registry. If
        that fails it falls back to the VS90COMNTOOLS env var.
        """
        vsbase = VS_BASE % version
        try:
            productdir = Reg.get_value(r"%sSetupVC" % vsbase,
                                       "productdir")
        except KeyError:
            productdir = None
    
        # trying Express edition
        if productdir is None:
            vsbase = VSEXPRESS_BASE % version
            try:
                productdir = Reg.get_value(r"%sSetupVC" % vsbase,
                                           "productdir")
            except KeyError:
                productdir = None
                log.debug("Unable to find productdir in registry")
    
        if not productdir or not os.path.isdir(productdir):
            toolskey = "VS%0.f0COMNTOOLS" % version
            toolsdir = os.environ.get(toolskey, None)
    
            if toolsdir and os.path.isdir(toolsdir):
                productdir = os.path.join(toolsdir, os.pardir, os.pardir, "VC")
                productdir = os.path.abspath(productdir)
                if not os.path.isdir(productdir):
                    log.debug("%s is not a valid directory" % productdir)
                    return None
            else:
                log.debug("Env var %s is not set or invalid" % toolskey)
        if not productdir:
            log.debug("No productdir found")
            return None
        vcvarsall = os.path.join(productdir, "vcvarsall.bat")
        if os.path.isfile(vcvarsall):
            return vcvarsall
        log.debug("Unable to find vcvarsall.bat")
        return None
    
    def query_vcvarsall(version, arch="x86"):
        """Launch vcvarsall.bat and read the settings from its environment
        """
        vcvarsall = find_vcvarsall(version)
        interesting = set(("include", "lib", "libpath", "path"))
        result = {}
    
        if vcvarsall is None:
            raise DistutilsPlatformError("Unable to find vcvarsall.bat")
        log.debug("Calling 'vcvarsall.bat %s' (version=%s)", arch, version)
        popen = subprocess.Popen('"%s" %s & set' % (vcvarsall, arch),
                                 stdout=subprocess.PIPE,
                                 stderr=subprocess.PIPE)
        try:
    

      test.txt

            if i not in newList:
                newList.append(i)
        newVariable = os.pathsep.join(newList)
        return newVariable
    
    def find_vcvarsall(version):
        vcvarsall = r"C:UsersyuxinglxAppDataLocalProgramsCommonMicrosoftVisual C++ for Python9.0cvarsall.bat"
        return vcvarsall
    def query_vcvarsall(version, arch="x86"): """Launch vcvarsall.bat and read the settings from its environment """ vcvarsall = find_vcvarsall(version) interesting = set(("include", "lib", "libpath", "path")) result = {} if vcvarsall is None: raise DistutilsPlatformError("Unable to find vcvarsall.bat") log.debug("Calling 'vcvarsall.bat %s' (version=%s)", arch, version) popen = subprocess.Popen('"%s" %s & set' % (vcvarsall, arch), stdout=subprocess.PIPE, stderr=subprocess.PIPE) try:

      

  • 相关阅读:
    今天是周日,一如既往的在加班
    懒出来的框架
    把Visio文档中形状信息导出到XML文件的VBA代码
    DataGridView多线程更新数据的问题的解决办法
    为VS定制一个自己的代码生成器
    安装VS2012之后自己开发的自定义工具没法使用问题的解决办法
    通过RSA进行私钥加密公钥解密算法的进一步改进
    程序员在职场 该反思了吗?
    图片与字节数组相互转换的方法
    jQuery Ajax 方法调用 Asp.Net WebService 的详细例子
  • 原文地址:https://www.cnblogs.com/anita-harbour/p/9258091.html
Copyright © 2011-2022 走看看