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 '\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 库)的更多相关文章
- python文件读取和写入案例
python文件读取和写入案例 直接上代码吧 都是说明 百度上找了很多,最终得出思路 没有直接可以读取修改的扩展,只能先读取,然后复制一份,然后在复制出来的文件里面追加保存 然后删除读的那个,但是缺 ...
- Python文件读取和数据处理
一.python文件读取 1.基本操作 读取文件信息时要注意文件编码,文件编码有UFT-8.ASCII或UTF-16等. 不过在python中最为常用的是UTF-8,所以如果不特别说明就默认UTF-8 ...
- Python 文件读取
1. 最基本的读文件方法: # File: readline-example-1.py file = open("sample.txt") while 1: line = file ...
- Linux下Python 文件内容替换脚本
Linux下Python 文件替换脚本 import sys,os if len(sys.argv)<=4: old_text,new_text = sys.argv[1],sys.argv[2 ...
- Python——文件读取
我们经常需要从文件中读取数据,因此学会文件的读取很重要,下面来介绍一下文件的读取工作: 1.读取整个文件 pi_digits.text 3.1415926535 8979323846 ...
- 初识python 文件读取 保存
上一章最后一题的答案:infors.sort(key=lambda x:x['age'])print(infors)--->[{'name': 'laowang', 'age': 23}, {' ...
- python文件读取
1.如何将一个“lessons.txt”文档一行行输出? myfile = file(‘lessons.txt’) for f in myfile.readlines(): print f myfil ...
- Python文件读取编码错误问题解决之(PyCharm开发工具默认设置的坑。。。)
刚接触Python学习,正准备做个爬虫的例子,谁知道代码一开始就遇到了一个大坑,狂汗啊. 问题是这样的:我通过代码爬取了博客园首页的HTML代码并存入到blog.txt文件当中,然后准备读取出来之后进 ...
- Python文件读取常用方法
1. 关于读取文件 f.read() 读取文件中所有内容 f.readline() 读取第一行的内容 f.readlines() 读取文件里面所有内容,把每行的内容放到一个list里面 注:因为文件指 ...
随机推荐
- Linux文件内容查看相关命令
1.more命令 在Linux中,more命令是一个基于vi编辑器的文本过滤器,它能以全屏的方式按页显示文本文件的内容,more里面内置了一些快捷键. (1)命令语法 more(选项)(参数) (2) ...
- 二叉树 & 平衡二叉树 算法(Java实现)
二叉树 比如我要依次插入10.3.1.8.23.15.28.先插入10作为根节点: 然后插入3,比10小,放在左边: 再插入1,比10和3小,放在3左边: 再插入8,比10小,比3大,放在3右边: 再 ...
- CentOS7使用tar.gz包安装MySql的踩坑之旅
由于客户的CentOS服务器没有安装yum工具,只能通过下载tar.gz包安装mysql,于是跟着万能的百度开启了漫漫踩坑之旅: 1.下载mysql-5.6.33-linux-glibc2.5-x86 ...
- AtCoder-arc059 (题解)
A - いっしょ / Be Together (结论/暴力) 题目链接 题目大意: 有 \(n\) 个数字,要将它们变成相等,对每一个数字最多操作一次,如将 \(a \to b\) 的代价为 \((a ...
- Django中使用CORS实现跨域请求(转)
原文:https://blog.csdn.net/zizle_lin/article/details/81381322 跨域请求: 请求url包含协议.网址.端口,任何一种不同都是跨域请求. ...
- SQL分类之DML:增删改表中的数据
DML:增删改表中的数据 1.添加数据: 语法: insert into 表名(列名1,列名2,...列名n) values(值1,值2,...值n): 注意: 1.列名和值要一一对应. 2.如果表名 ...
- Web负载均衡学习笔记之K8S内Ngnix微服务服务超时问题
0x00 概述 本文是从K8S内微服务的角度讨论Nginx超时的问题 0x01 问题 在K8S内部署微服务后,发现部分微服务链接超时,Connection Time Out. 最近碰到了一个 Ngin ...
- windows下vmware和Hyper-v共存方法
问题描述:环境:windows server 2012 r2系统下安装Hyper-v后,再安装Vmware 在Vmware中创建虚拟机,安装虚拟机系统的时候,vmware提示:VMware Works ...
- Java自学-数字与字符串 数学方法
Java Math类常用方法 java.lang.Math提供了一些常用的数学运算方法,并且都是以静态方法的形式存在 步骤 1 : 四舍五入, 随机数,开方,次方,π,自然常数 package dig ...
- MySQL里默认的几个库是干啥的?
本文涉及:MySQL安装后自带的4个数据库:information_schema. performance_schema.sys.mysql的作用及其中各个表所存储的数据含义 information_ ...