0

I try to delete from text file those characters '{a}' '{b}' ... and so on (I have 250 curly braces in the text file) using this code:

# -*- 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

because the letters change in every curly brackets i used asterisk in the,

but after i run this code nothing change in the text file:

>>> ================================ RESTART ================================
>>> 
>>>

i red:

but didn't found my solution.

Vy Do
  • 46,709
  • 59
  • 215
  • 313
newGIS
  • 598
  • 3
  • 10
  • 26

1 Answers1

1

Simplest, I believe, would be to use 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))