Python 替换文本中的某些词语
2024-10-20 08:44:05
https://stackoverflow.com/questions/39086/search-and-replace-a-line-in-a-file-in-python
from tempfile import mkstemp
from shutil import move
from os import fdopen, remove def replace(file_path, pattern, subst):
#Create temp file
fh, abs_path = mkstemp()
with fdopen(fh,'w') as new_file:
with open(file_path) as old_file:
for line in old_file:
new_file.write(line.replace(pattern, subst))
#Remove original file
remove(file_path)
#Move new file
move(abs_path, file_path)
最新文章
- java终端获取输入
- NoSql数据库初探-mongoDB读操作
- LeetCode 21 -- Merge Two Sorted Lists
- Hibernate学习总结
- latch: cache buffers chains故障处理总结(转载)
- Eclipse格式化代码快捷键失效问题
- Hibernate出现javax.naming.NoInitialContextException 错误的解决办法
- 拦路虎:jQuery
- Cython:基础教程(1) 语法
- sun.misc.unsafe类的使用
- UVALive 7454	Parentheses (栈+模拟)
- 响应式Web图形篇 —— icon fonts 的探析及应用
- 加密Web.Config配置文件
- java中抽象类的概念
- 《JavaScript高级程序设计》笔记:变量、作用域和内存问题(四)
- 如何把开源项目发布到Jcenter
- Code optimization and organization in Javascript / jQuery
- [UE4]C++ getter and setter
- ILMerge-GUI的使用
- 10分钟教你用Python打造微信天气预报机器人
热门文章
- ajax请求以及递归
- python 控制台输出美化
- STM32F103ZET6通用定时器
- 关于selenium定位元素时,出现此问题的处理办法:find_element=wait.until(ec.presence_of_element_locatetd(locator))定位不到页面元素的问题
- 【公告】请访问我Blog新站——superman2014 www.superman2014.com
- 中阶 d05 tomcat 安装 eclipse上配置tomcat
- springboot web项目创建及自动配置分析(thymeleaf+flyway)
- matplotlib Transform
- matplotlib TransformNode类
- python如何操作excel 基础代码