【发布时间】:2014-06-30 08:44:43
【问题描述】:
我有字节数组作为 str 并想发送它(如果我通过调试器查看它,它会告诉我 File.body 是 str)。出于这个原因,我必须创建要发送的消息。
request_text += '\n'.join([
'',
'--%s' % boundary_id,
attachment_headers,
File.body,
])
但仅在它尝试加入文件正文时,我收到异常:
UnicodeDecodeError: 'ascii' codec can't decode byte 0xff in position 0: ordinal not in range(128)
不过,在我拿到它的example 中,它是这样实现的。我应该如何命令 python 字符串工作字节字符串?我应该以某种方式对其进行解码吗?但是如何,如果那不是文本,而只是字节。
【问题讨论】:
-
好像是 unicode。是 Python 2 还是 3?
-
但调试器将主体参数显示为 {str}
-
这里的
attachment_headers是什么?boundary_id是什么类型?request_text是什么类型的? -
'\n'.encode('latin-1').join([ ...工作吗? -
@Will:那不会有所作为。在 Python 2 中,
'\n'已已编码。您会将字节字符串转换为 unicode 字符串(隐式解码),然后将再次编码回字节字符串。
标签: python string bytebuffer