python字符串替换为u

我有一个字符串。

m = 'I have two element. <U+2F3E> and <U+2F8F>'

我想替换为:

m = 'I have two element. u2F3E and u2F8F' # utf-8

我的代码:

import re

p1 = re.compile('<U++') # start "<"
p2 = re.compile('>+')    # end   ">"
m = 'I have two element. <U+2F3E> and <U+2F8F>'

out = pattern.sub('u', m) # like: 'I have two element. u2F3E> and u2F8F>'

但我收到此错误消息:

SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 0-1: truncated uXXXX escape

我该如何解决。谢谢。

回答

import re

m = 'I have two element. <U+2F3E> and <U+2F8F>'

print(re.sub(r'<U+(w+)>', r"u1", m))

# I have two element. u2F3E and u2F8F


以上是python字符串替换为u的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>