编码:TypeError:write()参数必须为str,而不是字节 [英] Encoding: TypeError: write() argument must be str, not bytes

查看:81
本文介绍了编码:TypeError:write()参数必须为str,而不是字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对python有基本的了解,但是在处理二进制编码问题上尚不清楚.我正在尝试从firefox-webextensions示例运行示例代码,在该示例中python脚本发送了JavaScript程序读取的文本.我一直遇到编码错误.

I have a rudimentary grasp of python but am not clear on dealing with binary encoding issues. I am trying to run sample code from a firefox-webextensions example in which a python script sends text that is read by a javascript program. I keep encountering encoding errors.

python代码是:

The python code is:

#! /Library/Frameworks/Python.framework/Versions/3.5/bin/python3.5
import sys, json, struct

text = "pong"

encodedContent = json.dumps(text)
encodedLength = struct.pack('@I', len(encodedContent))
encodedMessage = {'length': encodedLength, 'content': encodedContent}

sys.stdout.write(encodedMessage['length'])
sys.stdout.write(encodedMessage['content'])

错误跟踪(显示在firefox控制台中)为:

The error trace (displayed in firefox console) is:

stderr output from native app chatX: Traceback (most recent call last):
stderr output from native app chatX: File "/Users/inchem/Documents/firefox addons/py/chatX.py", line 10, in <module>
stderr output from native app chatX: sys.stdout.write(encodedMessage['length'])
stderr output from native app chatX: TypeError: write() argument must be str, not bytes

在OS X El Capitan 10.11.6,x86 64位cpu上运行python 3.5.1; firefox开发人员ed 52.0

Running python 3.5.1 on OS X El Capitan 10.11.6, x86 64bit cpu; firefox developer ed 52.0

如上所述,我正在使用的python脚本与原始脚本最小化了 https://developer.mozilla.org/zh-CN/Add- ons/WebExtensions/Native_messaging

The python script I am using, as shown above, is minimized from the original at https://developer.mozilla.org/en-US/Add-ons/WebExtensions/Native_messaging

我也尝试过:

sys.stdout.buffer.write(encodedMessage['length'])
sys.stdout.buffer.write(encodedMessage['content'])

生成的:

stderr output from native app chatX: sys.stdout.buffer.write(encodedMessage['content'])
stderr output from native app chatX: TypeError: a bytes-like object is required, not 'str'    

推荐答案

该示例可能与Python 2兼容,但是Python 3中的情况有所变化.

The example was probably Python 2-compliant, but things have changed in Python 3.

您正在使用以下形式生成长度为 bytes 的二进制表示形式:

You are generating a binary representation of the length as bytes with this:

encodedLength = struct.pack('@I', len(encodedContent))

它不可打印.您可以通过套接字流(它是二进制流)来编写它,而不能通过stdout(它是文本流)来编写.

It is not printable. You can write it through a socket stream which is a binary stream but not through stdout which is a text stream.

使用buffer的技巧(如

The trick of using buffer (as explained in How to write binary data in stdout in python 3?) is good but only for the binary part (note that you get the error message for the text part now):

sys.stdout.buffer.write(encodedMessage['length'])

对于文本部分,只需写入stdout:

For the text part, just write to stdout:

sys.stdout.write(encodedMessage['content'])

或将sys.stdout.buffer与字符串转换为字节:

or use sys.stdout.buffer with string to bytes conversion:

sys.stdout.buffer.write(bytes(encodedMessage['content'],"utf-8"))

这篇关于编码:TypeError:write()参数必须为str,而不是字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆