I am trying to replace each word after .
in the txt file below:
line1
line2
field: [orders.cancelled,orders.delivered,orders.reached
orders.pickup,orders.time]
some line
some line
I have a dictionary:
d = {'cancelled':'cancelled_at', 'deliver':'xxx'}
I am running the following code. However, I am getting the results for partial match ie
I see the new file has the following words
field: [orders.cancelled_at, orders.xxxed ..........
here from the word delivered the program is still replacing the first 7 words(deliver) and adding 'ed' in the end. I am not sure why
with open('list.txt', 'r') as g:
text = g.read()
for k in d:
before = f'.{k}'
after = f'.{d[k]}
#print(before)
#print(after)
text = text.replace(before, after)
#print(text)
with open('new_list.txt', 'w') as w:
w.write(text)
I also tried this one and I get the same results
import re
with open('list.txt', 'r') as f:
text = f.read()
for k in d:
before = f'.{k}(?!=\w)'
print(before)
after = f'.{d[k]}'
print(after)
text = re.sub(before, after, text)
with open('new_list.txt', 'w') as w:
w.write(text)
You can use
import re
d = {'cancelled':'cancelled_at', 'deliver':'xxx'}
rx = re.compile(fr"(?<=\.)(?:{'|'.join(d)})\b")
with open('list.txt', 'r') as f:
print( re.sub(rx, lambda x: d[x.group()], f.read()) )
See the Python demo
The regex generated by the code looks like
(?<=\.)(?:cancelled|deliver)\b
See the regex demo . Details:
(?<=\\.)
- a positive lookbehind that matches a location immediately preceded with a literal .
(?:cancelled|deliver)
- two alternatives: cancelled
or deliver
\\b
- as whole words, \\b
is a word boundary. The lambda x: d[x.group()]
replacement replaces the matched word with the corresponding dictionary key value.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.