试图了解“ TypeError:需要一个类似字节的对象,而不是'str'”

时间:2019-06-02 21:35:53

标签: python-3.x python-2.7

我对Python完全陌生,并尝试通过以下示例进行学习: 最好的学习方法是看示例,尝试理解和利用自己需要的代码。

我目前正在尝试通过与Raspberry Pi Zero W和adafruit fona 808突破板一起工作的项目来学习python。

但是,示例代码是为python 2.7制作的,我想将其转换为适用于python 3。

我收到此TypeError。 问题是,如果应该怎么做才能工作。 我曾尝试搜索过Google并仔细阅读,但不幸的是,我并不真正了解这是怎么回事。

有没有人可以向我解释这段代码实际上有什么问题,以及应该如何编写才能工作。 如果您还可以给我传递带有引用的链接,以便我可以阅读有关这些功能的信息,我将非常高兴。 :)

谢谢!

实际错误:

if "secondary DNS address" not in output1 and "locked" not in output1:
     

TypeError:需要一个类似字节的对象,而不是'str'

相关代码:

import I2C_LCD_driver
import socket
import fcntl
import struct
from os import system
import serial
import subprocess
from time import sleep
from ISStreamer.Streamer import Streamer

....
....
        output1 = subprocess.check_output("cat /var/log/syslog | grep pppd | tail -1", shell=True)
        if "secondary DNS address" not in output1 and "locked" not in output1:

1 个答案:

答案 0 :(得分:0)

您必须更改

if "secondary DNS address" not in output1 and "locked" not in output1:

if b"secondary DNS address" not in output1 and b"locked" not in output1:

每当出现此错误时,请尝试在“”之前添加b