在re进行字符串处理是,有时会出现如下问题bad escape \U 获取其它问题类似问题,如下所示;
经过跟如re类发现,是由于\\U的转义造成的;
1. 示例
import re
def demo_re():
xlsPath = r'C:\Users\Administrator'
lines = r'''
set patchRomPath="J:/Bluetooth/"
set xlsPath="C:\Users\Administrator\Desktop"
'''
_rul = 'xlsPath="{}"'.format(xlsPath)
_pat = re.compile(r'xlsPath=\".+\"')
return _pat.sub(_rul, lines)
print(demo_re())
直接执行就会出现上述截图问题,要进行比较的修改,注意其中的\\转义标记附
2.修改
import re
def demo_re():
xlsPath = r'C:\Users\Administrator'
lines = r'''
set patchRomPath="J:/Bluetooth/"
set xlsPath="C:\Users\Administrator\Desktop"
'''
_rul = 'xlsPath="{}"'.format(xlsPath.replace('\\', '/'))
_pat = re.compile(r'xlsPath=\".+\"')
return _pat.sub(_rul, lines)
print(demo_re())
输出结果