【发布时间】:2016-04-30 10:11:06
【问题描述】:
在 Python 2.6.8 中编写 unicode 字符串时出现以下错误:
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'))
包含 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
【问题讨论】:
标签: file unicode utf-8 bytearray python-2.6