37,720
社区成员
发帖
与我相关
我的任务
分享
>>> str1 = "hello"
>>> str1
'hello'
>>> str2 = u"hello"
>>> str2
u'hello'
>>> bytes1 = bytearray(str1)
>>> bytes1
bytearray(b'hello')
>>> bytes2 = bytearray(str2)
Traceback (most recent call last):
File "<pyshell#6>", line 1, in <module>
bytes2 = bytearray(str2)
TypeError: unicode argument without an encoding
>>> bytes2 = bytearray(str2, 'utf')
>>> bytes2
bytearray(b'hello')
>>>
这里如果只是英文,比较难看出差别。
建议了解下str,unicode和encode的知识。