如何在Python中使用正则表达式替换文件中的字符串,因为我想打开一个文件,我在其中替换其他字符串的字符串,我们需要使用Reg表达式(搜索和替换).任何人都可以帮助我吗?打开文件并将其与搜索和替换方法一起使用的一些示例
Qui*_*inn 14
# The following code will search 'mm/dd/yyyy' (e.g. NOV/30/2016 ),
# and replace with 'mm-dd-yyyy' in multi-line mode.
import re
with open ('input.txt', 'r' ) as f:
content = f.read()
content_new = re.sub('(\w{2})/(\d{2})/(\d{4})', r'\1-\2-\3', content, flags = re.M)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
21375 次 |
| 最近记录: |