如何将str转换为unicode?

时间:2018-08-24 03:55:35

标签: string python-3.x unicode python-3.5

文件中的内容如下:

This is a Japanese character: \u3046

我想将上面的字符串转换成这种形式:

This is a Japanese character: unicodeValue_3046|unidecoded_u

这是我的代码:

def my_repl(match):
   return ' unicodeValue_' + match.group('uni')[2:] + '|unidecoded_' +unidecode(match.group('uni'))

re.sub(pattern=r'(?P<uni>\\u[a-f0-9]{4})', repl=my_repl, string=open('ja.txt', 'r').readline())

我得到的不是我期望的:

Out[207]: u'This is a Japanese character:  unicodeValue_3046|unidecoded_\\u3046 '

将其写入文件后:

opt = re.sub(pattern=r'(?P<uni>\\u[a-f0-9]{4})', repl=my_repl, string=open('ja.txt', 'r').readline())
codecs.open('op', 'w', 'utf-8').write(opt)

我看到的是: This is a Japanese character: unicodeValue_3046|unidecoded_\u3046

然后unidecode不起作用,它仅输出给出的内容。

我知道:unidecode(u'\u3046')unidecode('\u3046')是'u',但就我而言,这是不同的。

有人可以帮我解决吗?谢谢!

0 个答案:

没有答案