我尝试使用以下代码从文本文件中删除那些字符'{a}''{b}'...依此类推(我在文本文件中有250个花括号):
# -*- coding: cp1255 -*-
import sys,codecs,string
reload(sys)
sys.setdefaultencoding('utf8')
root = r"G:\desktop\y\test2.txt"
x = open(root)
s=x.read().replace('{*}','').replace('-','')
x.close()
x=open(root,"w")
x.write(s)
x.close
因为字母在我使用星号的每个大括号中都改变了,所以
但是运行此代码后,文本文件中没有任何变化:
>>> ================================ RESTART ================================
>>>
>>>
我红色:
但没有找到我的解决方案。
我认为,最简单的方法是使用regular expressions
import re
data = '''{a} Four score and seven years ago our fathers brought forth on this continent, {b} a new nation, {c} conceived in Liberty, {d} and dedicated to the proposition that all men are created equal.
'''
pattern = re.compile(r'\{[A-Za-z]\}')
print(pattern.sub('{*}', data))