Python替换txt格式的文本
需求:就是需要把数据库脚本中的:COLLATE 'Chinese_PRC_CI_AS’替换为空,并把替换后文件保存回源文件上
import re
# 打开文件并读取内容
with open('new 5.txt', 'r', encoding='utf-8') as file:
content = file.read()
# 使用正则表达式删除特定COLLATE的内容
pattern = r"COLLATE 'Chinese_PRC_CI_AS'"
content = re.sub(pattern, '', content)
# 将处理后的内容写回文件
with open('new 5.txt', 'w', encoding='utf-8') as file:
file.write(content)
print("成功!")