---
title: "python编码处理：unicode字节串转成中文 各种字符串举例说明"
author: "yangze"
date: "2010-11-16T13:58:00Z"
tags: []
slug: "python-unicode"
draft: false
lastmod: "2010-11-16T13:58:00Z"
id: 842054
url: "https://yangze.iteye.com/blog/842054"
---

编码问题一直是很头痛的问题：

 

**当字符串是：'\\u4e2d\\u56fd'** 

&gt;&gt;&gt;s=\['\\u4e2d\\u56fd','\\u6e05\\u534e\\u5927\\u5b66']  
&gt;&gt;&gt;str=s\[0].decode('unicode\_escape')  #.encode("EUC\_KR")  
&gt;&gt;&gt;print str

中国

 

**当字符串是:' &#19996;&#20122;&#23398;&#22242;&#19968;&#20013;'**

&gt;&gt;&gt;print unichr(19996)

东

**ord()支持unicode，可以显示特定字符的unicode号码，如：**

```
>>>print ord('A')
65
```

**只要和Unicode连接，就会产生Unicode字串。如：**

```
>>> 'help'
'help'
>>> 'help，' + u'python'     
u'help，python'
```

 

**对于ASCII(7位)兼容的字串，可和内置的str()函数把Unicode字串转换成ASCII字串。**如：

```
>>> str(u'hello world')
'hello world'
```

 

 

对几个概念的理解：

ASCII码:    用数据字 对应 相应的字符

[![image](http://images.cnblogs.com/cnblogs_com/yangze/WindowsLiveWriter/pythonunicode_C467/image_thumb_1.png)](http://images.cnblogs.com/cnblogs_com/yangze/WindowsLiveWriter/pythonunicode_C467/image_4.png)

 

而中文 就是区位码对应汉字    如：“好” 的ASCII码为： 22909

 

unicode 编码 每个国家分一块。   它有UTF-8、UTF-16、UTF-32等形式

中文范围 4E00-9FBF：  这个范围内有 gbk,gb2312,

 

utf-8是基于unicode的 国际化的场合适合使用  
gb2312和gb2312都是国标码 出现的较早 主要用于编解码常用汉字