python 3.x 中 stdin.write() 的格式化字符串 [英] formatting strings for stdin.write() in python 3.x

查看:86
本文介绍了python 3.x 中 stdin.write() 的格式化字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试使用 python 3.2.2 执行此代码时遇到错误

I'm having a problem where I get errors when I try to execute this code with python 3.2.2

working_file = subprocess.Popen(["/pyRoot/iAmAProgram"], stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE)

working_file.stdin.write('message')

我知道 python 3 改变了它处理字符串的方式,但我不明白如何格式化消息".有谁知道我如何更改此代码以使其有效?

I understand that python 3 changed the way it handles strings but I dont understand how to format the 'message'. Does anyone know how I'd change this code to be valid?

非常感谢

乔恩

更新:这是我收到的错误信息

update: heres the error message i get

Traceback (most recent call last):
  File "/pyRoot/goRender.py", line 18, in <module>
    working_file.stdin.write('3')
TypeError: 'str' does not support the buffer interface

推荐答案

您的错误信息是TypeError: 'str' 不支持缓冲区接口"吗?该错误消息几乎准确地告诉您出了什么问题.您不会将字符串对象写入该 sdtin.那你写什么?好吧,任何支持缓冲区接口的东西.通常这是字节对象.

Is your error message "TypeError: 'str' does not support the buffer interface"? That error message tells you pretty much exactly what is wrong. You don't write string objects to that sdtin. So what do you write? Well, anything supporting the buffer interface. Typically this is bytes objects.

喜欢:

working_file.stdin.write(b'message')

这篇关于python 3.x 中 stdin.write() 的格式化字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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