扫码关注官方订阅号
python项目,运行某个脚本,更新在一个已存在的文档中某段内容。目前想到的是利用该文档中内容的前后关键词定位,清空这些行,然后写入新的内容。
认证0级讲师
可以利用正则表达式。
with open('filename', 'w+') as f: line = f.read().splitlines() for i in range(len(line)): if 'xxx' in line[i]: # line[i] = 'xxx' line[i].replace('xxx', 'new xxx') # 或者用正则 f.writelines(line)
微信扫码关注PHP中文网服务号
QQ扫码加入技术交流群
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
PHP学习
技术支持
返回顶部
可以利用正则表达式。