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 '\n'.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:\Users\yuxinglx\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0\vcvarsall.bat"\n'''
add_str2 = " return vcvarsall\n\n"
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"%s\Setup\VC" % vsbase,
"productdir")
except KeyError:
productdir = None # trying Express edition
if productdir is None:
vsbase = VSEXPRESS_BASE % version
try:
productdir = Reg.get_value(r"%s\Setup\VC" % 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:\Users\yuxinglx\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.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:

  

python文件读取,替换(带格式,python lib 库)的更多相关文章

  1. python文件读取和写入案例

    python文件读取和写入案例  直接上代码吧 都是说明 百度上找了很多,最终得出思路 没有直接可以读取修改的扩展,只能先读取,然后复制一份,然后在复制出来的文件里面追加保存 然后删除读的那个,但是缺 ...

  2. Python文件读取和数据处理

    一.python文件读取 1.基本操作 读取文件信息时要注意文件编码,文件编码有UFT-8.ASCII或UTF-16等. 不过在python中最为常用的是UTF-8,所以如果不特别说明就默认UTF-8 ...

  3. Python 文件读取

    1. 最基本的读文件方法: # File: readline-example-1.py file = open("sample.txt") while 1: line = file ...

  4. Linux下Python 文件内容替换脚本

    Linux下Python 文件替换脚本 import sys,os if len(sys.argv)<=4: old_text,new_text = sys.argv[1],sys.argv[2 ...

  5. Python——文件读取

    我们经常需要从文件中读取数据,因此学会文件的读取很重要,下面来介绍一下文件的读取工作:     1.读取整个文件     pi_digits.text 3.1415926535 8979323846 ...

  6. 初识python 文件读取 保存

    上一章最后一题的答案:infors.sort(key=lambda x:x['age'])print(infors)--->[{'name': 'laowang', 'age': 23}, {' ...

  7. python文件读取

    1.如何将一个“lessons.txt”文档一行行输出? myfile = file(‘lessons.txt’) for f in myfile.readlines(): print f myfil ...

  8. Python文件读取编码错误问题解决之(PyCharm开发工具默认设置的坑。。。)

    刚接触Python学习,正准备做个爬虫的例子,谁知道代码一开始就遇到了一个大坑,狂汗啊. 问题是这样的:我通过代码爬取了博客园首页的HTML代码并存入到blog.txt文件当中,然后准备读取出来之后进 ...

  9. Python文件读取常用方法

    1. 关于读取文件 f.read() 读取文件中所有内容 f.readline() 读取第一行的内容 f.readlines() 读取文件里面所有内容,把每行的内容放到一个list里面 注:因为文件指 ...

随机推荐

  1. excel中怎么将行转换为列及列转换成行

    操作方法 01 选中要把行转换为列的数据.然后点击键盘上的ctrl+c. 02 在需要放置的单元格上,右键点击,然后点击菜单上的‘选择性粘贴’. 03 在弹出的窗口上,点击勾选上‘转置’.点击确定按钮 ...

  2. [转帖]中国 GPL 诉讼第一案:关于 GPL 问题的探讨

    中国 GPL 诉讼第一案:关于 GPL 问题的探讨 https://linux.cn/article-11683-1.html 2019 年 11 月初,数字天堂(北京)网络技术有限公司(下称:数字天 ...

  3. SpringBoot扩展点之三:SpringBootServletInitializer扩展

    SpringBootServletInitializer 熟悉了SpringApplication的原理之后,我们再来了解SpringBootServletInitializer的原理就比较容易了. ...

  4. idea Autowired 提示红色的解决方式

  5. 《C程序设计语言》(K&R)中文高清非扫描件

    <C程序设计语言>(K&R)中文高清非扫描件(带书签目录) 对于某下载东西都要C币的网站无爱了.好不容易找了一个,发出来看会不会帮到别人   附上addr:https://pan. ...

  6. 【Pandas教程】像写SQL一样用Pandas~

    写在最前 Python在数据分析领域有三个必须需要熟悉的库,分别是pandas,numpy和matplotlib,如果排个优先级的话,我推荐先学pandas. numpy主要用于数组和矩阵的运算,一般 ...

  7. Prometheus监控学习笔记之Grafana用户权限分组

    Grafana有账户分权和管理功能,不过和一般的账户分权管理操作不一样.Grafana通过admin账户建立organization,在不通的org内配置数据源和用户,用户隶属于不通的org,只能看到 ...

  8. Identity和IdentityServer的区别及联系

    关于Identity和IdentityServer初学的时候可能会有一些疑惑(虽然我也不是很精深吧),但是,这里说一下自己关于这两者的一些理解,如有错误,欢迎指正 总体上, ASP.NET Core ...

  9. winform实现Session功能(保存用户信息)

    问题描述:在winform中想实现像BS中类似Session的功能,放上需要的信息,在程序中都可以访问到. 解决方案:由于自己很长时间没有做过winform的程序,一时间竟然手足无措起来.后来发现wi ...

  10. 使用SqlBulkCopy将DataTable百万级数据瞬间入库

    #region 使用SqlBulkCopy将DataTable中的数据批量插入数据库中 /// <summary> /// 注意:DataTable中的列需要与数据库表中的列完全一致.// ...