用列表中的值逐行替换文件中符合条件的字符串,并保存为新的文件, 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. LeetCode-838 推多米诺

    来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/push-dominoes 题目描述 n 张多米诺骨牌排成一行,将每张多米诺骨牌垂直竖立.在开始时 ...

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

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

  3. Solidity8.0-01

    对应崔棉大师 1-25课程https://www.bilibili.com/video/BV1yS4y1N7yu/?spm_id_from=333.788&vd_source=c81b130b ...

  4. Servlet简介和ServletContext

    0x01: 什么是Servlet? 是sun公司开发动态web的技术 实现了servlet接口的Java程序 0x02: Servlet的实现类有哪些? Servlet接口默认有两个实现类 HttpS ...

  5. 52道常见Python面试题,你都看过了吗?(转发供参考学习)

    https://blog.csdn.net/xiaohei3ge/article/details/88080284?utm_medium=distribute.pc_relevant.none-tas ...

  6. Apache druid笔记

    Apache Duid学习笔记2 1.历史节点的查询效率与内存数据比成正比,内存越大则读取磁盘的次数越少, 历史节点内存越大总数据量越小则查询速度越快. 2.缓存机制可以选择外部和内部缓存,外部缓存如 ...

  7. Prometheus 特点

    1.1 Prometheus的特点 Prometheus是一个开源的完整监控解决方案,其对传统监控系统的测试和告警模型进行了彻底的颠覆,形成了基于中央化的规则计算.统一分析和告警的新模型. 相比于传统 ...

  8. leecode75. 颜色分类

    75. 颜色分类 给定一个包含红色.白色和蓝色.共 n 个元素的数组 nums ,原地对它们进行排序,使得相同颜色的元素相邻,并按照红色.白色.蓝色顺序排列. 我们使用整数 0. 1 和 2 分别表示 ...

  9. Leaflet加载GeoServer发布的WMTS地图服务

    leaflet本身并不支持WMTS服务,需要借助leaflet-tilelayer-wmts插件实现,但是插件是为通用WMTS服务实现的.在使用的过程中出现了无法调用的问题,这里进行了稍微修改. 加载 ...

  10. LCP 03.机器人大冒险

    def robot(command, obstacles, x, y): xx = 0 yy = 0 tmp = [] for c in command: if c == 'U': yy += 1 i ...