python - 使用正则表达式替换文件中的子字符串和python
问题描述
假设我有一个带有
aaaa = /bbb/ccc/ddd/eee/fff 行的 file.txt
我想用 xxx 替换 ccc
我尝试了下一个但没有成功
import re
import fileinput
for line in fileinput.input(file.txt, inplace=True):
re.sub(r'(ccc)', 'xxx', line)
解决方案
尝试这个:
m = re.sub(r'ccc', r'xxx', "/bbb/ccc/ddd/eee/fff")
#OUTPUT: /bbb/xxx/ddd/eee/fff
推荐阅读
- python - knn图像分类,准确率差
- awkward-array - 保存笨拙1数组的最佳方法?
- android - 通话中对 Android 音频路由有何影响?
- android - Android CameraX 返回成功但不保存图片
- python - 解密在 O(N) 时间内运行的基数排序的字符串排序版本
- python - 使用 Gekko 的模拟模式 - 初始值为零的变量 C2 (NH2Cl) 不起作用 - 为什么?
- r - 将单个表中的两列合并为一列
- c# - HtmlRendererCore.PdfSharpCore 弹性框
- google-play - 在 Google Play 和 App Store 中获取当前应用版本
- r - 如何在ggplot的图形上写出某个变量的值?