Skip to content

Instantly share code, notes, and snippets.

@HQMIS
Created January 6, 2013 17:10
Show Gist options
  • Select an option

  • Save HQMIS/4468718 to your computer and use it in GitHub Desktop.

Select an option

Save HQMIS/4468718 to your computer and use it in GitHub Desktop.
utf8转换为gb2312报错, 原因:gb2312对一些很生僻的中文字并不支持,比如 “囧” “苶”
Python 2.7.2 |EPD 7.2-2 (32-bit)| (default, Sep 14 2011, 11:02:05) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> err = u'\u6c39'
>>> print err
氹
>>> err.encode("gb2312")
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
err.encode("gb2312")
UnicodeEncodeError: 'gb2312' codec can't encode character u'\u6c39' in position 0: illegal multibyte sequence
>>> err.encode("GBK")
'\x9a\xeb'
>>> err.encode("GB18030")
'\x9a\xeb'
>>>
@HQMIS

HQMIS commented Jan 24, 2013

Copy link
Copy Markdown
Author

Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
==== No Subprocess ====

print u'\u2764'
❤
u'\u2764'.encode('gb2312')
Traceback (most recent call last):
File "<pyshell#1>", line 1, in
u'\u2764'.encode('gb2312')
UnicodeEncodeError: 'gb2312' codec can't encode character u'\u2764' in position 0: illegal multibyte sequence

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment