Python: .encode方法
2013-11-29 16:53
169 查看
官方教程: http://docs.python.org/2/howto/unicode.html
方法.encode([encoding],
[errors='strict']), 返回一个 8-bit string version of the Unicode string, encoded in the requested encoding.
The errors parameter is the same as the parameter of the unicode() constructor, with one additional possibility; as well as ‘strict’,‘ignore’,
and ‘replace’, you can also pass ‘xmlcharrefreplace’ which uses XML’s character references. 以下的例子说明了其不同的结果:
方法.encode([encoding],
[errors='strict']), 返回一个 8-bit string version of the Unicode string, encoded in the requested encoding.
The errors parameter is the same as the parameter of the unicode() constructor, with one additional possibility; as well as ‘strict’,‘ignore’,
and ‘replace’, you can also pass ‘xmlcharrefreplace’ which uses XML’s character references. 以下的例子说明了其不同的结果:
>>> u = unichr(40960) + u'abcd' + unichr(1972) >>> u.encode('utf-8') '\xea\x80\x80abcd\xde\xb4' >>> u.encode('ascii') Traceback (most recent call last): ... UnicodeEncodeError: 'ascii' codec can't encode character u'\ua000' in position 0: ordinal not in range(128) >>> u.encode('ascii', 'ignore') 'abcd' >>> u.encode('ascii', 'replace') '?abcd?' >>> u.encode('ascii', 'xmlcharrefreplace') 'ꀀabcd'
相关文章推荐
- python的str,unicode对象的encode和decode方法
- Python字符串的encode与decode研究心得 乱码问题解决方法
- Python中encode()方法的使用简介
- Python字符串的encode与decode研究心得乱码问题解决方法
- Python UnicodeEncodeError: 'gbk' codec can't encode character 解决方法
- python的str,unicode对象的encode和decode方法
- Python的Ftplib问题:UnicodeEncodeError: 'latin-1' codec can't encode characters的解决方法
- python的str,unicode对象的encode和decode方法
- Python UnicodeEncodeError: 'gbk' codec can't encode character 解决方法
- python的str,unicode对象的encode和decode方法(转)
- Python字符串的encode与decode研究心得乱码问题解决方法
- Python字符串的encode与decode研究心得乱码问题解决方法
- Python字符串的encode与decode研究心得乱码问题解决方法
- python的str,unicode对象的encode和decode方法
- Python字符串的encode与decode研究心得乱码问题解决方法
- python的str,unicode对象的encode和decode方法(转)
- Python字符串的encode与decode研究心得乱码问题解决方法
- python的str,unicode对象的encode和decode方法
- 浅淡Python 3.x中encode、decode方法
- Python UnicodeEncodeError: 'gbk' codec can't encode character 解决方法