Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 使用echo命令获取TypeError必须是str而不是字节_Python 3.x - Fatal编程技术网

Python 3.x 使用echo命令获取TypeError必须是str而不是字节

Python 3.x 使用echo命令获取TypeError必须是str而不是字节,python-3.x,Python 3.x,我正在升级到py3,并且有与py2兼容的代码,该代码现在抛出一个错误TypeError:必须是str,而不是bytes 在脚本中,它发送带有编码字符串的shell echo命令 "echo \"" + base64.b64encode(b'Hello World') + "\" | base64 -d" 我如何修改以上内容使其通过 谢谢您还可以将字符串转换为字节,然后对其进行解码: bytes\u string=b“echo\”+ba

我正在升级到py3,并且有与py2兼容的代码,该代码现在抛出一个错误TypeError:必须是str,而不是bytes

在脚本中,它发送带有编码字符串的shell echo命令

"echo \"" + base64.b64encode(b'Hello World') + "\" | base64 -d"
我如何修改以上内容使其通过


谢谢

您还可以将字符串转换为字节,然后对其进行解码:

bytes\u string=b“echo\”+base64.b64encode(b'Hello World')+b“\”base64-d
打印(字节\字符串解码('utf-8'))
>>>echo“SGVsbG8gV29ybGQ=“| base64-d

Hi,我尝试了上面的代码,但它给了我AttributeError:“bytes”对象没有属性“string\u decode”下划线在bytes和string之间,而不是string和decode