摘要:
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问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
TypeError: cannot concatenate ‘str’ and ‘int’ objects print str + int 的时候就会这样了 python + 作为连接符的时候,不会自动给你把int转换成str 补充知识:TypeError: cannot concatenate ‘str’ and ...
Python 出现错误TypeError: ‘NoneType’ object is not iterable解决办法 TypeError: ‘NoneType’ object is not iterable 这个错误提示一般发生在将None赋给多个值时。 def myprocess(): a == b if a != b: ...
TypeError: list indices must be integers or slices, not str 这个错误,这里就分析一下为什么会报错以及如何解决。 这个错误的意思是’类型错误:list的索引必须是’integers’或者’slices’不能是’str’ 我...
TypeError: notify.sendNotifybyWxPucher is not a function!
主要介绍了Vue 报错TypeError: this.$set is not a function 的解决方法,分享给大家,需要的朋友们下面随着小编来一起学习学习吧
主要介绍了JQuery报错"Uncaught TypeError: Illegal invocation"的处理方法,需要的朋友可以参考下
TypeError: Unexpected keyword argument passed to optimizer: learning_rate 3.问题定位: 先看报错代码:大概意思是, 传给优化器的learning_rate参数错误。 模型训练是在服务器Linux环境下进行的,之后在...
主要介绍了Ajax方式上传文件报错"Uncaught TypeError: Illegal invocation",非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
在调整loss计算的时候遇到了TypeError: only integer tensors of a single element can be converted to an index这个问题,原来的计算公式为: self.loss_D = (self.loss_D_fake + self.loss_D_real) * 0.5 调整后...
翻译过来是类型为“type”的TypeError对象没有len(),我报错的代码是: #coding=utf-8 print(请输入一个字符串:) a = input('') #回文的长度至少为2 if len(str) < 2: print('请不要输入空字符串!') a = ...
本文研究的主要是Python使用pickle模块存储数据报错解决方法,以代码的形式展示,具体如下。 首先来了解下pickle模块 pickle提供了一个简单的持久化功能。...TypeError: write() argument must be str, not byte
Uncaught TypeError: Cannot assign to read only property 'exports' of object '#<Object>' 点开错误的文件,标注错误的地方是这样的一段代码: import {normalTime} from './timeFormat'; module....
为大家分享了一个解决jquery插件:TypeError:$.browser is undefined报错的方法,解决报错问题的方法也很简单,需要解决此类问题的朋友不要错过这篇文章。