- 论坛徽章:
- 11
|
本帖最后由 timespace 于 2014-05-23 14:58 编辑
还好有bytearray,不是太复杂,完整尝试过程:- Python 2.7.5 (default, Mar 9 2014, 22:15:05)
- [GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin
- Type "help", "copyright", "credits" or "license" for more information.
- >>> a = "帖子"
- >>> a
- '\xe5\xb8\x96\xe5\xad\x90'
- >>> b = unicode(a)
- Traceback (most recent call last):
- File "<stdin>", line 1, in <module>
- UnicodeDecodeError: 'ascii' codec can't decode byte 0xe5 in position 0: ordinal not in range(128)
- >>> b = u'\xe5\xb8\x96\xe5\xad\x90'
- >>> b
- u'\xe5\xb8\x96\xe5\xad\x90'
- >>> print b
- 帖å-
- >>> [ord(e) for e in b]
- [229, 184, 150, 229, 173, 144]
- >>> bytearray(ord(e) for e in b)
- bytearray(b'\xe5\xb8\x96\xe5\xad\x90')
- >>> bytearray(ord(e) for e in b).decode('utf-8')
- u'\u5e16\u5b50'
- >>> c = bytearray(ord(e) for e in b).decode('utf-8')
- >>> c
- u'\u5e16\u5b50'
- >>> print c
- 帖子
- >>> d = c.encode('utf-8')
- >>> d
- '\xe5\xb8\x96\xe5\xad\x90'
- >>>
复制代码 |
|