用列表中的值逐行替换文件中符合条件的字符串,并保存为新的文件, 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逐行读取替换文件中的字符串的更多相关文章

  1. 使用 sed 命令查找和替换文件中的字符串的 16 个示例

    当你在使用文本文件时,很可能需要查找和替换文件中的字符串.sed 命令主要用于替换一个文件中的文本.在 Linux 中这可以通过使用 sed 命令和 awk 命令来完成. 在本教程中,我们将告诉你使用 ...

  2. linux sed 批量替换文件中的字符串或符号

    sed -i :直接修改读取的文件内容,而不是输出到终端.   sed -i 就是直接对文本文件进行操作的   替换每行第一次出现的字符串 sed -i 's/查找的字符串/替换的字符串/' 文件   ...

  3. 新手C#s.Split(),s.Substring(,)以及读取txt文件中的字符串的学习2018.08.05

    s.split()用于字符串分割,具有多种重载方法,可以通过指定字符或字符串分割原字符串成为字符串数组. //s.Split()用于分割字符串为字符串数组,StringSplitOptions.Rem ...

  4. python 逐行读取txt文件

    逐行读取txt文件 path = r'D:\123456\1.txt'with open(path, 'r', encoding='utf-8') as f:    for line in f:   ...

  5. shell 脚本替换文件中某个字符串

    1.将当前目录下包含jack串的文件中,jack字符串替换为tom sed -i "s/jack/tom/g" `grep "jack" -rl ./` 2.将 ...

  6. grep和sed替换文件中的字符串

    sed -i s/"str1"/"str2"/g `grep "str1" -rl --include="*.[ch]" ...

  7. grep和sed替换文件中的字符串【转】

    sed -i s/"str1"/"str2"/g `grep "str1" -rl --include="*.[ch]" ...

  8. shell 脚本sed替换文件中某个字符串

    有些大文件,特别的大.有几百兆,甚至更大. 用文本编辑器打开十分的费劲,电脑都卡死了. 想替换其中的字符串,很麻烦. 这个时候有了shell,简直强大到爆炸! # du -h user.sql 304 ...

  9. Bat 替换文件中的字符串

    echo off setlocal enabledelayedexpansion set "file=Config\__Config\server_config_common.xml&quo ...

  10. python 小程序,替换文件中的字符串

    [root@PythonPC ~]# cat passwd root:x:::root:/root:/bin/bash bin:x:::bin:/bin:/sbin/nologin daemon:x: ...

随机推荐

  1. ChatGPT Java客户端,OpenAi的Java版本SDK已完成,请火速接入。

    已经支持OpenAI官方的全部api,有bug欢迎朋友们指出,互相学习. 源码地址:https://github.com/Grt1228/chatgpt-java 不对之处欢迎指正. 注意:由于这个接 ...

  2. C++ cannot bind non-const lvalue reference of type ‘Dog&’ to an rvalue of type ‘Dog’

    void function(Dog & d){ /************** } 调用这个函数,如果传参一个右值对象,临时对象,则会出现这个问题 一个临时对象的引用,这怎么想都不合理 从该函 ...

  3. 【博图scl语言】313-2dp

    ①如果 if(***) then *** := ***; end_if; ②循环 for n1:=1 to 50 by 1 do end_for; WHILE #n1 < 54 DO END_W ...

  4. MySQL线程池、连接池等概念

    一.MySQL连接池 1 连接池通常实现在client端,是指应用(客户端)预先创建一定的连接,利用这些连接服务于客户端所有的DB请求. 2 如果某一个时刻,空闲的连接数小于DB的请求数,则需要将请求 ...

  5. 2.2 在resources目录下,新建applicationContext.xml

    <?xml version="1.0" encoding="UTF-8"?> <beans> <!-- 这个bean标签的作用是, ...

  6. HttpClient 提交 JSON数据

    常见数据格式 application/x-www-form-urlencoded 这也是最常见的 POST 提交数据的方式,一般是用来提交表单. multipart/form-data 上传文件时通常 ...

  7. (面试题) 面试官:如何在forEach的循环里使用break

    大家都知道 js 的 forEach里是不能使用break.但是为什么不能在forEach里使用呢?在forEach里使用break 会发生什么呢? 一. 在forEach里使用break 会发生什么 ...

  8. docker 二进制安装

    首先所属环境为内网并且服务器拥有的开发环境不确定,需要跑当前服务所需代码,所以优先选择使用docker docker 文档地址 https://docs.docker.com 在 install 中存 ...

  9. CentOS6.8安装docker教程

    在VMware新安装CentOS6.8系统 CentOS6.8可在阿里镜像库下载: https://mirrors.aliyun.com/centos-vault/6.8/isos/x86_64/ 在 ...

  10. 错误提示“com.alibaba.fastjson.JSONException: exepct '[', but string, pos 4, json”解决

    1.错误提示信息如下: com.alibaba.fastjson.JSONException: exepct '[', but string, pos 4, json : "[{" ...