摘要:
If you use Python3x then string
is not the same type as for Python 2.x, you must cast it to bytes (encode it).
In python 3, bytes strings and unicode strings are now two different types. Whenever you have a unicode string that you need to use as a byte string, you need to encode()
it. And when you have a byte string, you need to decode
it to use it as a regular (python 2.x) string.
================================================================
windows vista 系统 Eclipse+PyDev 环境
如下代码在 python2.7 环境下运行良好:
# coding=gbk # 如果不加这个,输出的中文是乱码 import subprocess cmd = "cmd.exe" ip = "172.29.69." for i in range(139, 145) : p = subprocess.Popen(cmd, shell = True, stdout = subprocess.PIPE, stdin = subprocess.PIPE, stderr = subprocess.PIPE) cmdstr = "ping 172.29.69." + str(i) + "\n" p.stdin.write(cmdstr) p.stdin.close() p.wait() print("execution result : %s" % p.stdout.read()) #end for
代码出处:
[1] http://yinzhezq.blog.163.com/blog/static/164862890201241224219884/
但在 python3.3 环境下就会运行出错:
Traceback (most recent call last):
File "xxx\ping.py", line 14, in <module>
p.stdin.write(cmdstr)
TypeError: 'str' does not support the buffer interface
================================================================
解决方法:
将代码修改为(代码编辑器不能改格式,真蛋疼)
# coding=GBK
import subprocess
cmd = "cmd.exe"
ip = "172.29.69."
for i in range(139, 145) :
p = subprocess.Popen(cmd, shell = True,
stdout = subprocess.PIPE,
stdin = subprocess.PIPE,
stderr = subprocess.PIPE)
cmdstr = "ping 172.29.69." + str(i) + "\n"
#p.stdin.write(cmdstr)
p.stdin.write(bytes(cmdstr, "UTF-8"))
p.stdin.close()
p.wait()
#print("execution result : %s" % p.stdout.read())
print("execution result : %s" % p.stdout.read().decode("gb2312"))
#end for
解决方法出处:
[1] http://stackoverflow.com/questions/11781639/typeerror-str-does-not-support-buffer-interface
[2] http://stackoverflow.com/questions/5471158/typeerror-str-does-not-support-the-buffer-interface
相关推荐
TypeError: loop of ufunc does not support argument 0 of type Tensor which has no callable arctan method 报错行 self.u = np.round(np.dot(self.F, self.u)) #或者 self.u = np.round(self.u + np.dot(K, ...
### 解决Python中报错TypeError: must be str, not bytes问题 #### 一、问题背景与常见场景 在Python编程过程中,尤其是处理文件操作时,可能会遇到“TypeError: must be str, not bytes”这一错误。这通常发生在...
在Python编程过程中,可能会遇到一个常见的错误提示"TypeError: 'list' object is not callable"。这个错误通常是由于程序员不小心将内置的数据结构名称如`list`、`tuple`等作为变量名,导致后续尝试调用这些内置...
TypeError: notify.sendNotifybyWxPucher is not a function!
在使用Qt 5.8版本时,可能会遇到一个常见的错误,即`TypeError: Property 'asciify' of object Core`。这个错误通常发生在尝试访问或使用`Core`对象的`asciify`属性时,而该属性在当前环境中并未定义或者不支持。这...
TypeError: _queue_reduction(): incompatible function arguments. The following argument types are supported: 1. (process_group: torch.distributed.ProcessGroup, grads_batch: List[List[at::Tensor]], ...
总结起来,遇到`TypeError: list indices must be integers or slices, not str`时,首先要检查你是否在尝试访问一个列表或ResultSet,如果是,请确保使用正确的索引来访问其中的元素。同时,了解并熟练掌握...
然而,在使用jQuery过程中,开发者有时会遇到JavaScript控制台抛出“Uncaught TypeError: Illegal invocation”错误。这个错误通常不是由jQuery自身引起的,而是与JavaScript作用域及上下文有关。 当我们进行Ajax...
主要介绍了Django 错误:TypeError at / 'bool' object is not callable解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
TypeError: Unexpected keyword argument passed to optimizer: learning_rate 3.问题定位: 先看报错代码:大概意思是, 传给优化器的learning_rate参数错误。 模型训练是在服务器Linux环境下进行的,之后在...
在Python编程过程中,可能会遇到一个常见的异常:`TypeError: cannot concatenate 'str' and 'int' objects`。这个异常通常发生在尝试合并(concatenate)字符串(str)和整型(int)数据类型时,因为Python不支持...