【问题标题】:TypeError: cannot use a string pattern on a bytes-like object : Pyexpect In Python3TypeError:不能在类似字节的对象上使用字符串模式:Python3 中的 Pyexpect
【发布时间】:2021-10-21 05:38:51
【问题描述】:

我正在尝试将 pexpect 模块(版本 4.8.0)与 Python 3.6.8 一起使用。我收到一个错误

TypeError: cannot use a string pattern on a bytes-like object

这是我的代码:

buff = BytesIO()
child = pexpect.spawn(path, args, logfile=buff, timeout=self.PASSPHRASE_TIMEOUT, **kwargs)
while True:
     idx = child.expect([self.PASSPHRASE_RE,pexpect.EOF])
     if idx == 0:
        child.sendline(passphrase)
     elif idx == 1:
          child.wait()
          break

我在idx = child.expect([self.PASSPHRASE_RE,pexpect.EOF]) 行中遇到错误

注意:我已经尝试过 StackOverflow 上的一些解决方案,例如:

  1. 在 pexpect.spawn 中传递 encoding('utf-8') 参数。
  2. 将 pexpect.spawn 替换为 pexpect.spawnu

但没有运气再次出现同样的错误。

请帮助我,我已经浪费了 3 天时间来解决此错误。

【问题讨论】:

    标签: python python-3.x python-2.7 python-3.6 pexpect


    【解决方案1】:

    问题在于 PASSPHRASE_RE 类型,即 sre.SRE_Pattern。

    我只是使用 PASSPHRASE_RE.pattern

    将其转换为字符串

    【讨论】:

      猜你喜欢
      • 2014-02-21
      • 1970-01-01
      • 2016-10-09
      • 1970-01-01
      • 1970-01-01
      • 2015-09-10
      • 2016-06-03
      • 1970-01-01
      相关资源
      最近更新 更多