Program reads a text file and replaces a matched word based on a variable. The problem is that non-exact matches are being replaced. So if I'm replacing "the" with "da" the word "then" becomes "dan"
for fn in os.listdir('.'):
if os.path.isfile(fn):
if fn.endswith(".txt"):
s = open(fn).read()
for i in skills:
link = skills[i]
s = s.replace(i, "<a href=\"%s\">%s</a>" %(link,i), 1)
print "%s updated" %fn
f = open(fn, 'w')
f.write(s)
f.close()
The issue is at s = s.replace(i, "<a href=\"%s\">%s</a>" %(link,i), 1)
where the first argument in s.replace() is the variable containing the string I want to match for.
I've tried using word boundaries s = s.replace(r'\b'+i+r'\b', "<a href=\"%s\">%s</a>" %(link,i), 1)
and formatting the value at i
into a new variable: regex = r'\b' + i + r'\b'
and using regex
as the first argument in s.replace
but it doesn't work.
Note. skills
is a dictionary containing word/link pairs
Since s
is a string, it won't support regular expression operations.
To use regular expressions on your strings, you need to do that explicitly.
So put at the start of your code
import re
and where you now use
s = s.replace(i, "<a href=\"%s\">%s</a>" %(link,i), 1)
use, instead,
s = re.sub(r'\b'+i+r'\b', '<a href="%s">%s</a>'%(link,i), s, 1)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With