0
在Python 2.6.8我写一个Unicode字符串时,遇到下列错误:Python的2.6:1的参数必须是字符串或固定缓冲区,而不是字节组的字节组UTF-8字符串的
Traceback (most recent call last):
File "test.py", line 10, in <module>
f.write(bytearray(u, 'utf_8'))
TypeError: argument 1 must be string or pinned buffer, not bytearray
当运行在Python 2.7.8中的代码一切正常,字符串打印和写入正确。
这是代码:
#!/usr/bin/python
# -*- coding: utf-8 -*-
u = u"Möwe"
print u
with open("testout", "w") as f:
f.write(bytearray(u, 'utf_8'))
相同的行为occures包含4字节的UTF-8字符的字符串。
Python 2.6中的二进制信息:
$ python26 -v -c 'exit' 2>&1 | grep -A 1 '^Python'
Python 2.6.8 (unknown, Nov 7 2012, 14:47:45)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-52)] on linux2