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

时间:2018-07-08 14:19:50

标签: python python-3.x

我正在使用py-espeak-ng尝试运行以下代码:

esng = ESpeakNG(voice='english-us')
esng.pitch = 32
esng.speed = 150
esng.say('Hello World!', sync=True)

但是我得到这个错误:

esng.say('Hello World!', sync=True)
Traceback (most recent call last): File "<stdin>", line 1, in <module> 
  File "C:\User\AppData\Local\Programs\Python\Python36\lib\site-packages\espeakng\__init__.py", line 103, in say
    self._espeak_exe(args, sync=sync)
  File "C:\Users\AppData\Local\Programs\Python\Python36\lib\site-packages\espeakng\__init__.py", line 68, in _espeak_exe
    stderr=subprocess.STDOUT)
  File "C:\Users\AppData\Local\Programs\Python\Python36\lib\subprocess.py", line 709, in __init__
    restore_signals, start_new_session)
  File "C:\Users\AppData\Local\Programs\Python\Python36\lib\subprocess.py", line 971, in _execute_child
    args = list2cmdline(args)
  File "C:\Users\AppData\Local\Programs\Python\Python36\lib\sub`enter code here`process.py", line 461, in list2cmdline
    needquote = (" " in arg) or ("\t" in arg) or not arg
  TypeError: a bytes-like object is required, not 'str'

1 个答案:

答案 0 :(得分:0)

Python有两种字符串:unicode和字节字符串。在python 3中, normal 字符串(str)是unicode字符串,但是某些API需要字节字符串。

错误消息:

TypeError: a bytes-like object is required, not 'str'

证明API期望一个字节字符串并获得一个unicode字符串。当错误在esng.say('Hello World!', sync=True)处出现时,表示esng.say需要一个字节字符串。

您应该写:

esng.say('Hello World!'.encode(), sync=True)