【问题标题】:getting Unicode decode error while using gTTS in python在 python 中使用 gTTS 时出现 Unicode 解码错误
【发布时间】:2018-02-08 23:27:05
【问题描述】:
在 python 2.x 中使用 gTTS 谷歌翻译模块时,我遇到了错误-
文件
"/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/gtts/tts.py",
第 94 行,在 init 中
如果 self._len(text)
尽管我在 python 脚本中包含了# -*- coding: utf-8 -*-,但在使用非 ASCII 字符时出现错误。告诉我一些其他的实现方式,比如我可以用英语写句子并翻译成其他语言。但这也不起作用,因为我正在用英语讲话,只是改变了口音。
我到处搜索了很多,但找不到答案。请帮忙!
【问题讨论】:
标签:
text-to-speech
python-2.x
translate
【解决方案1】:
我尝试过以 unicode 格式编写一个字符串——
u'Qu'est-ce que tu fais? Gardez-le de côté.'.
ASCII 码字符被转换为 unicode 格式,从而解决错误。所以,你想要转成语音的文本甚至可以有 utf-8 格式的字符,并且可以很容易地转换。
【解决方案2】:
您还需要解码 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")
适合我,试一试。