Given a textile how can I replace all the tokens that have % at the beginning for []. For instance in the following text file:
Hi how are you?
I %am %fine.
Thanks %and %you
How can I enclose all the characters with % with []:
Hi how are you?
I [am] [fine].
Thanks [and] [you]
I tried to first filter the tokens and then replace them but maybe there is a more pythonic way:
with open('../file') as f:
s = str(f.readlines())
a_list = re.sub(r'(?<=\W)[$]\S*', s.replace('.',''))
a_list= set(a_list)
print(list(a_list))