Unicode字符串可以用多种方式编码为普通字符串, 依照你所选择的编码(encoding):
<!-- Inject Script Filtered --> Toggle line numbers

    #将Unicode转换成普通的Python字符串:"编码(encode)"
    unicodestring = u"Hello world"
    utf8string = unicodestring.encode("utf-8")
    asciistring = unicodestring.encode("ascii")
    isostring = unicodestring.encode("ISO-8859-1")
    utf16string = unicodestring.encode("utf-16")

    #将普通的Python字符串转换成Unicode: "解码(decode)"
   plainstring1 = unicode(utf8string, "utf-8")
   plainstring2 = unicode(asciistring, "ascii")
   plainstring3 = unicode(isostring, "ISO-8859-1")
   plainstring4 = unicode(utf16string, "utf-16")

   assert plainstring1==plainstring2==plainstring3==plainstring4

讨论 Discussion
If you find yourself dealing with text that contains non-ASCII characters, you have to learn about Unicode梬hat it is, how it works, and how Python uses it.
如果你发现自己正在处理包含非ASCII码字符的文本, 你必须学习Unicode,关于它是什么,如何工作,而且Python如何使用它。

Unicode is a big topic.Luckily, you don't need to know everything about Unicode to be able to solve real-world problems with it: a few basic bits of knowledge are enough.First, you must understand the difference between bytes and characters.In older, ASCII-centric languages and environments, bytes and characters are treated as the same thing.Since a byte can hold up to 256 values, these environments are limited to 256 characters.Unicode, on the other hand, has tens of thousands of characters.That means that each Unicode character takes more than one byte, so you need to make the distinction between characters and bytes.
Unicode是一个大的主题。幸运地,你并不需要知道关于Unicode码的每件事,就能够用它解决真 实世界的问题: 一些基本知识就够了。首先,你得了解在字节和字符之间的不同。原先,在以ASCII码为中心的语言和环境中,字节和字符被当做相同的事物。由于一个字节只 能有256个值,这些环境就受限为只支持 256个字符。Unicode码,另一方面,有数万个字符,那意谓着每个Unicode字符占用多个字节,因此,你需要在字符和字节之间作出区别。

Standard Python strings are really byte strings, and a Python character is really a byte.Other terms for the standard Python type are "8-bit string" and "plain string.",In this recipe we will call them byte strings, to remind you of their byte-orientedness.
标准的Python字符串确实是字节字符串,而且一个Python字符真的是一个字节。换个术语,标准的 Python字符串类型的是 "8位字符串(8-bit string)"和"普通字符串(plain string)". 在这一份配方中我们把它们称作是字节串(byte strings), 并记住它们是基于字节的。

Conversely, a Python Unicode character is an abstract object big enough to hold the character, analogous to Python's long integers.You don't have to worry about the internal representation;the representation of Unicode characters becomes an issue only when you are trying to send them to some byte-oriented function, such as the write method for files or the send method for network sockets.At that point, you must choose how to represent the characters as bytes.Converting from Unicode to a byte string is called encoding the string.Similarly, when you load Unicode strings from a file, socket, or other byte-oriented object, you need to decode the strings from bytes to characters.
相反地,一个Python Unicode码字符是一个大到足够支持(Unicode)字符的一个抽象对象, 类似于Python中的长整数。 你不必要为内在的表示担忧; 只有当你正在尝试把它们传递给给一些基于字节的函数的时候,Unicode字符的表示变成一个议题, 比如文件的write方法或网络套接字的send 方法。那时,你必须要选择该如何表示这些(Unicode)字符为字节。从Unicode码到字节串的转换被叫做编码。同样地,当你从文件,套接字或其他 的基于字节的对象中装入一个Unicode字符串的时候,你需要把字节串解码为(Unicode)字符。

There are many ways of converting Unicode objects to byte strings, each of which is called an encoding.For a variety of historical, political, and technical reasons, there is no one "right" encoding.Every encoding has a case-insensitive name, and that name is passed to the decode method as a parameter. Here are a few you should know about:
将Unicode码对象转换成字节串有许多方法, 每个被称为一个编码(encoding)。由于多种历史的,政治上的,和技术上的原因,没有一个 "正确的"编码。每个编码有一个大小写无关的名字,而且那一个名字被作为一个叁数传给解码方法。这里是一些你应该知道的:
    The UTF- encoding can handle any Unicode character.It  file, and a UTF- file that happens to use only ASCII characters  very backward-compatible, especially with older Unix tools.UTF- is far and away the dominant encoding on Unix.It's primary weakness is that it is fairly inefficient for Eastern texts.
    UTF- 编码能处理任何的Unicode字符。它也是与ASCII码向后兼容的,因此一个纯粹的ASCII码文件也能被考虑为一个UTF- 文件,而且一个碰巧只使用ASCII码字符的 UTF- 文件和拥有同样字符的ASCII码文件是相同的。 这个特性使得UTF-8的向后兼容性非常好,尤其使用较旧的 Unix工具时。UTF- 无疑地是在 Unix 上的占优势的编码。它主要的弱点是对东方文字是非常低效的。
    The UTF- encoding  .
    UTF- 编码在微软的操作系统和Java环境下受到偏爱。它对西方语言是比较低效,但对于东方语言是更有效率的。一个 UTF- 的变体有时叫作UCS- 。
    The ISO- series of encodings are -character ASCII supersets.They cannot support all of the Unicode characters;they can support only some particular language or family of languages.ISO--, also known , covers most Western European and African languages, but not Arabic.ISO--, also known ,covers many Eastern European languages such as Hungarian and Polish.
    ISO-8859编码系列是256个字符的ASCII码的超集。他们不能够支援所有的Unicode码字符; 他们只能支援一些特别的语言或语言家族。ISO--, 也既Latin-,包括大多数的西欧和非洲语言, 但是不含阿拉伯语。ISO--,也既Latin-,包括许多东欧的语言,像是匈牙利语和波兰语。

If you want to be able to encode all Unicode characters, you probably want to use UTF-.You will probably need to deal with the other encodings only when you are handed data in those encodings created by some other application.
如果你想要能够编码所有的Unicode码字符,你或许想要使用UTF-。只有当你需要处理那些由其他应用产生的其它编码的数据时,你或许才需要处理其他编码。

Python: 在Unicode和普通字符串之间转换的更多相关文章

  1. Python——在Unicode和普通字符串之间转换

    1.1. 问题 Problem You need to deal with data that doesn't fit in the ASCII character set. 你需要处理不适合用ASC ...

  2. Python 16进制与字符串的转换

    电脑上装了Python2.7和3.3两个版本,平时运行程序包括在Eclipse里面调试都会使用2.7,但是由于某些原因在cmd命令行中输入python得到的解释器则是3.3, 一直没对此做处理,因为这 ...

  3. UNICODE和ANSI字符串的转换(解释了MultiByteToWideChar,WideCharToMultiByte,GetTextCharsetInfo,GetTextCharset,IsDBCSLeadByte,IsDBCSLeadByteEx,IsTextUnicode一共7个函数)

    继上集故事<多字符集(ANSI)和UNICODE及字符串处理方式准则 >,我们现在有一些特殊需求: 有时候我们的字符串是多字符型,我们却需要使用宽字符型:有的时候却恰恰相反. Window ...

  4. python 在Unicode和普通字符串 str 之间转换

    unicodestring = u"Hello world" # 将Unicode转化为普通Python字符串:"encode" utf8string = un ...

  5. Ansi、Unicode、UTF8字符串之间的转换和写入文本文件

    转载请注明出处http://www.cppblog.com/greatws/archive/2008/08/31/60546.html 最近有人问我关于这个的问题,就此写一篇blog Ansi字符串我 ...

  6. python 字典、列表、字符串 之间的转换

    1.列表与字符串转换 1)列表转字符串: 将列表中的内容拼接成一个字符串 将列表中的值转成字符串 2)字符串转列表: 用eval转换 将字符串每个字符转成列表中的值 将字符串按分割成列表 2.列表与字 ...

  7. 【Python】bytes和hex字符串之间的相互转换。

    反复在几个环境上折腾码流的拼装解析和可读化打印,总是遇到hex字符串和bytes之间的转换,记录在这里吧. 1. 在Python2.7.x上(更老的环境真心折腾不起),hex字符串和bytes之间的转 ...

  8. Python时间,日期,时间戳之间转换,时间转换时间戳,Python时间戳转换时间,Python时间转换时间戳

    #1.将字符串的时间转换为时间戳方法: a = "2013-10-10 23:40:00" #将其转换为时间数组 import time timeArray = time.strp ...

  9. char*、string、CString各种字符串之间转换

    参考博客: http://blog.csdn.net/luoweifu/article/details/20242307 http://blog.csdn.net/luoweifu/article/d ...

随机推荐

  1. Android进程的内存管理分析

    尊重原创作者,转载请注明出处: http://blog.csdn.net/gemmem/article/details/8920039 最近在网上看了不少Android内存管理方面的博文,但是文章大多 ...

  2. OpenCV---图片生成视频

    /** It is a batch processing interface. */ #include "stdafx.h" #include <windows.h> ...

  3. zoj 2376 Ants

    #include<stdio.h> #include<stdlib.h> ]; int main(void) { int t,n,m,i,len,max,min,mx,mi; ...

  4. MD5随机散列加密算法

    项目中需要在登录验证用户名.密码的时候对密码进行加密处理,由于是比较商业化的软件,所以安全方面还是必须要考虑的.而使用MD5随机散列加密算法使得密码加密后不可逆,很大程度上提升了安全性.废话不多说,看 ...

  5. php知识(第2天)

    运算符 PHP中运算符一共分为9类: 赋值运算符, 算术运算符,比较运算符, 逻辑运算符, 错误抑制符, 三目运算符, 位运算符, 自操作运算符, 连接操作符 算术运算符 算术运算: 基本运算符: + ...

  6. 【转】DevExpress控件安装

    原文链接: DevExpress控件安装.汉化使用教程 - 田园里的蟋蟀 学习网址: 1.DevExpress控件中文网 2.DevExpress控件中文网使用教程 3.DevExpress控件使用经 ...

  7. datatable,查询,排序,复制等操作

    DataTable排序,检索,合并详解 一.排序 获取DataTable的默认视图 对视图设置排序表达式 用排序后的视图导出的新DataTable替换就DataTable (Asc升序可省略,多列排序 ...

  8. [LeetCode]题解(python):105-Construct Binary Tree from Preorder and Inorder Traversal

    题目来源: https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/ 题意分析: ...

  9. Google日历添加农历、节日和天气插件(步骤)

    Google日历添加农历.节日和天气插件(步骤) Google功能非常多,Google日历只是其中一个,而且支持Exchange账户(iPhone,WP7,诺基亚等)和Google账户登录(andro ...

  10. 如何在自己的Activity中去控制EditText的焦点

    在进入一个Activity时,如果这个Activity中有EditText,则这个EditText会自动获取焦点,然后就会弹出软键盘,这样给用户体验不是很好.所以一般会通过代码控制让EditText不 ...