i am not able to figure out why the below two print statements gives different results. can any one please explain me? i am just giving a small sample example. in the first print statement table is getting replaced by a special character and the second one gives correct one.
import re
def tblcnv( str ):
rtval = re.sub("table", "chair", str)
return rtval
rval = "<table is in the place where you sit daily "
tblcnt = re.sub(r"<(table.*place)", tblcnv('1円'), rval)
print tblcnt
print tblcnv("<table is in the place where you sit daily")
3 Answers 3
This line:
tblcnt = re.sub(r"<(table.*place)", tblcnv('1円'), rval)
probably doesn't do what you think it should. The call to tblcnv('1円') returns '1円' which is the smiley face you see and then it replaces the first chunk of your string with said smiley face.
Comments
When defining tblcnt, you're passing tblcnv('1円'). '1円' is the string containing a single byte with value 0x01. So the result from tblcnv is that same string.
1 Comment
'\1円' or r'1円'.According to the re.sub manual it takes a function which "is called for every non-overlapping occurrence of pattern". As those occurrences are actually match objects you are best of using a simple lambda expression which extracts the group(1) of the match objects and passes it to your tblcnv function.
import re
def tblcnv( str ):
rtval = re.sub("table", "chair", str)
return rtval
rval = "<table is in the place where you sit daily "
tblcnt = re.sub(r"<(table.*place)", lambda m: tblcnv(m.group(1)), rval)
print tblcnt
print tblcnv("<table is in the place where you sit daily")
D:\Python>tbl.py☺ where you sit daily<chair is in the place where you sit daily