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: ...
随机推荐
- LeetCode-622 设计循环队列
来源:力扣(LeetCode)链接:https://leetcode.cn/problems/design-circular-queue 题目描述 设计你的循环队列实现. 循环队列是一种线性数据结构, ...
- Spring cloud Alibaba Nacos服务注册发现和配置中心
Nacos(官方网站:http://nacos.io)是一个易于使用的平台,旨在用于动态服务发现,配置和服务管理.它可以帮助您轻松构建云本机应用程序和微服务平台. Nacos = Eureka + c ...
- [转载]pytest报AttributeError: module ‘pytest‘ has no attribute ‘main‘
转自:https://blog.csdn.net/yinying12/article/details/110522989 pytest报AttributeError: module 'pytest' ...
- C语言中static关键字用法
概述 static关键字在c语言中比较常用,使用恰当能够大大提高程序的模块化特性,有利于扩展和维护. 在程序中使用static 变量 1. 局部变量 普通局部变量是再熟悉不过的变量了,在任何一个函数内 ...
- Windows10安装VMware
系统环境: Windows 10 安装步骤: 1.下载vmware https://my.vmware.com/cn/web/vmware/downloads 2.安装(这里安装语言我选择的是中文) ...
- 报错:cannot import name ‘escape’ from ‘jinja2’
jinja2版本问题导致 解决方法: 降低版本即可 pip3 install Jinja2==3.0.3 -U pip3 install werkzeug==2.0.3 -U jinja2介绍 jin ...
- FileChannel 数据传输(文件拷贝)
import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import ...
- phpstorm 本地代码更新与服务器同步
第一步: 第二步: 在第二步的时候在 ip之后的 testsftp 测试一下 看是否能连接到服务器 第三步: 第四步:
- redisTemplate实现分布式锁(释放锁用lua脚本)
package com.xxx.platform.util; import org.springframework.beans.factory.annotation.Autowired; import ...
- .NET Framework 中对webapi进行jwt验证
最近在项目中对webapi进行了jwt验证,做一个记录 有关于jwt生成和验证token的操作全部记录在jwthelper.cs文件中: /// <summary> /// 授权JWT类 ...