Unicode 字符串转 Unicode 字符,Python 3 [英] Unicode string to Unicode character, Python 3

查看:45
本文介绍了Unicode 字符串转 Unicode 字符,Python 3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Python 3.x 进行编程.假设我有以下 Unicode 字符串:

I'm programming using Python 3.x. Say I have the following Unicode string:

my_string =' \xed\x95\x9c'

'\xed\x95\x9c' 实际上是韩文字符 的 UTF-8 字节流.将 my_string 转换为 的最简单方法是什么?my_string.decode('utf-8') 不起作用,因为 my_string 是 Unicode 字符串,而不是字节字符串.

'\xed\x95\x9c' is actually the UTF-8 byte stream for the Korean character . What's the easiest way to convert my_string to ? my_string.decode('utf-8') doesn't work because my_string is a Unicode string, not a byte string.

推荐答案

有许多可能的 encode/decode 链可以导致预期的结果.这是一个:

There are many possible encode/decode chains which lead to the desired result. Here is one:

In [257]: '\xed\x95\x9c'.encode('latin-1').decode('utf-8')
Out[257]: '한'

这是我用来找到这个编码/解码链的代码.

这篇关于Unicode 字符串转 Unicode 字符,Python 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆