python逐行读取替换文件中的字符串
用列表中的值逐行替换文件中符合条件的字符串,并保存为新的文件, open("file").readlines
方案1: 逐行替换并保存为新的文件
import re
def replace_string1():
new_name_list = ["string1", "string11", "string111",...]
# 正则提取
p_s0 = 'def test_TC_(.*?)\('#get accurate String2 at position:def test_TC_String2(
#逐行替换file中String1 with String2
f2 = open("./file_new.py", 'w', encoding='utf-8')
lines = open("./file.py", encoding='utf-8').readlines()
# 逐行查找file中是否包含String2, 找到之后用用列表中的值逐一替换
j = 0
for s in lines:
casename = re.findall(p_s0, s, re.S) #逐行查找file中是是否包含String2, 找到返回列表["String2"], 否则返回[]
if (casename):
while j < len(new_name_list):
s = s.replace(casename[0],new_name_list[j]) # file 行中包含String2, 则替换成列表中的值
j += 1
break
f2.write(s)
f2.close()
方案2: 逐行替换并保存文件
import re
def replace_string2():
with open("./testlistrule.py", 'r+', encoding='utf-8') as f:
content = f.read()
# 正则提取
p_s1 = 'self.case_id = (.*?)\n'
p_s0 = 'def test_TC_(.*?)\('
id = re.findall(p_s1, content, re.S)
casename = re.findall(p_s0, content, re.S)
new_name_list = []
for i in range(len(casename)):
new_casename = casename[i] + "_" + id[i]
new_name_list.append(new_casename)
with open("./testlistrule.py", 'r', encoding='utf-8') as f:
lines = f.readlines()
j = 0
content = []
for s in lines:
casename = re.findall(p_s0, s, re.S)
if (casename):
while j < len(new_name_list):
s = s.replace(casename[0],new_name_list[j])
j += 1
break
content.append(s)
with open("./testlistrule.py", 'w+', encoding='utf-8') as f:
f.write(''.join(content))
f.close()
python逐行读取替换文件中的字符串的更多相关文章
- 使用 sed 命令查找和替换文件中的字符串的 16 个示例
当你在使用文本文件时,很可能需要查找和替换文件中的字符串.sed 命令主要用于替换一个文件中的文本.在 Linux 中这可以通过使用 sed 命令和 awk 命令来完成. 在本教程中,我们将告诉你使用 ...
- linux sed 批量替换文件中的字符串或符号
sed -i :直接修改读取的文件内容,而不是输出到终端. sed -i 就是直接对文本文件进行操作的 替换每行第一次出现的字符串 sed -i 's/查找的字符串/替换的字符串/' 文件 ...
- 新手C#s.Split(),s.Substring(,)以及读取txt文件中的字符串的学习2018.08.05
s.split()用于字符串分割,具有多种重载方法,可以通过指定字符或字符串分割原字符串成为字符串数组. //s.Split()用于分割字符串为字符串数组,StringSplitOptions.Rem ...
- python 逐行读取txt文件
逐行读取txt文件 path = r'D:\123456\1.txt'with open(path, 'r', encoding='utf-8') as f: for line in f: ...
- shell 脚本替换文件中某个字符串
1.将当前目录下包含jack串的文件中,jack字符串替换为tom sed -i "s/jack/tom/g" `grep "jack" -rl ./` 2.将 ...
- grep和sed替换文件中的字符串
sed -i s/"str1"/"str2"/g `grep "str1" -rl --include="*.[ch]" ...
- grep和sed替换文件中的字符串【转】
sed -i s/"str1"/"str2"/g `grep "str1" -rl --include="*.[ch]" ...
- shell 脚本sed替换文件中某个字符串
有些大文件,特别的大.有几百兆,甚至更大. 用文本编辑器打开十分的费劲,电脑都卡死了. 想替换其中的字符串,很麻烦. 这个时候有了shell,简直强大到爆炸! # du -h user.sql 304 ...
- Bat 替换文件中的字符串
echo off setlocal enabledelayedexpansion set "file=Config\__Config\server_config_common.xml&quo ...
- python 小程序,替换文件中的字符串
[root@PythonPC ~]# cat passwd root:x:::root:/root:/bin/bash bin:x:::bin:/bin:/sbin/nologin daemon:x: ...
随机推荐
- 内容分发网络 CDN
介绍 CDN 内容分发网络(英语:Content Delivery Network 或 Content Distribution Network,缩写:CDN)是建立并覆盖在承载网上,由不同区域的服务 ...
- tabs 滑动效果
https://www.zhangxinxu.com/wordpress/2020/07/css-var-improve-components/ 伪类选择器 https://blog.csdn.net ...
- vue 退出登录页面跳转 浏览器localStorage中的值没有改变
vuex中的loginout方法actions:{ loginout(data:string){ return new Promise(((resolve, reject)=>{ setTime ...
- Golang依赖管理工具: go module 详解
Golang依赖管理工具: go module (go1.11+) 大多数语言都会有包管理工具,像Node有npm,PHP有composer,Java有Maven和Gradle. 可是,Go语言一直缺 ...
- 浅谈zookeeper
zookeeper用来解决高可用问题,具有高可用,高性能,具有严格的顺序(只要是分布式系统就会是一个严格的顺序)访问控制能力的分布式协调服务,做分布式协调的作用,可以做服务的同步,维护配置文件和命名服 ...
- WDA学习(26):Phase Indicator使用
1.19 UI Element:Phase Indicator使用 本实例测试创建Phase Indicator; 运行结果: 1.创建Component,View: V_PHASE_IND; 2.创 ...
- 069_VisualForce Ajax 更多用法解读02
一.同步调用的异常处理:AJAX Toolkit try catch... <html> <head> <script src="/soap/ajax/44. ...
- 在SublimeText3中想使用快捷键调出插件ColorPicker不起作用办法解决
在SublimeText3中快速打开插件ColorPicker的快捷健是Ctrl+Shift+C,但是我试了却不管用,后来发现是跟插件ConvertToUTF8插件中的快捷键冲突导致,知道原因就好办了 ...
- Postman中的测试脚本(Test scripts)
一.postman测试脚本 测试脚本是在发送请求之后运行的,并且已经从服务器接收到响应. 二.测试举例 1.设置环境变量 pm.environment.set("variable_key&q ...
- ssgvip设置
ssgvip.tpl set service "{vipport}" protocol {xieyi} src-port 0-65535 dst-port {vipport}-{v ...