2017-08-31 85 views
3

使用腹围在使用腹围谷歌翻译模块在Python 2.x中,我得到错误 -得到的Unicode解码错误而在蟒蛇

File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/gtts/tts.py", line 94, in init if self._len(text) <= self.MAX_CHARS: File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/gtts/tts.py", line 154, in _len return len(unicode(text)) UnicodeDecodeError: 'ascii' codec can't decode byte 0xe0 in position 0: ordinal not in range(128)`.

即使我已经包括在我的python脚本# -*- coding: utf-8 -*-,我得到关于使用非ASCII字符的错误。告诉我一些其他的实现方式,比如我可以用英语写句子并用其他语言翻译。但是,由于我正在用英语发言,所以这也不起作用,只改变口音。

我已经搜遍很多地方,但无法找到答案。请帮忙!

回答

1

我曾尝试以unicode格式编写一个字符串作为- u'Qu'est-ce que tu fais? Gardez-le de côté.'

将ASCII码字符转换为unicode格式,从而解决错误。所以,你想要转换成语音的文本甚至可以有utf-8格式的字符,并且可以很容易地转换。

0

您还需要输入参数的gtts-cli.py

变化从这个以下行解码:

if args.text == "-": 
    text = sys.stdin.read() 
else: 
    text = arg.text 

这样:

if args.text == "-": 
    text = sys.stdin.read() 
else: 
    text = codecs.decode(arg.text,"utf-8") 

对我的作品, 试一试。