Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
1
Help us understand the problem. What is going on with this article?
@mikika

py-translator を使ってみる(IndexError: list index out of rangeエラーの回避)

More than 1 year has passed since last update.

https://pypi.org/project/py-translator/ に従って、installして実行してみた。

https://pypi.org/project/py-translator/ より引用:

Google Translate - Python Library. The end goal is a simple application for translating text in the terminal. Text can be generated interactively or programmatically in the shell environment.

環境

$ python --version
Python 3.6.7

インストール

$ pip install py_translator==1.8.9

実行

$ python
Python 3.6.7 (default, Nov 15 2018, 16:29:08)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from py_translator import Translator
>>> s = Translator().translate(text='Hello my friend', dest='es').text  <== スペイン語に翻訳
>>> print(s)
Hola mi amigo
>>> s = Translator().translate(text='Hello my friend', dest='ja').text  <== ja指定してみる
>>> print(s)
こんにちは、友よ

エラーが出る場合

実際のところ、install直後は、次のようなエラーが出ていた

$ python
Python 3.6.7 (default, Nov 15 2018, 16:29:08)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from py_translator import Translator
>>> s = Translator().translate(text='Hello my friend', dest='es').text
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/python/lib/python3.6/site-packages/py_translator/client.py", line 172, in translate
    data = self._translate(text, dest, src)
  File "/usr/local/python/lib/python3.6/site-packages/py_translator/client.py", line 75, in _translate
    token = self.token_acquirer.do(text)
  File "/usr/local/python/lib/python3.6/site-packages/py_translator/gtoken.py", line 184, in do
    self._update()
  File "/usr/local/python/lib/python3.6/site-packages/py_translator/gtoken.py", line 56, in _update
    self.tkk = self.RE_TKK.findall(r.text)[0]
IndexError: list index out of range

Google updated how token is displayed in response. #8
に従って、

/usr/local/python/lib/python3.6/site-packages/py_translator/gtoken.py
を以下のように修正。

gtoken.py
    RE_TKK = re.compile(r'TKK=\'([^\']*)\';', re.DOTALL)  <--- この行を消して
    RE_TKK = re.compile(r'tkk:\'(.+?)\'', re.DOTALL)  <--- この行を追加

参考:py_translator stopped working with “IndexError: list index out of range”

1
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
mikika
ここでの発言は私個人の見解であり、所属する会社&組織の見解を反映したものではありません。
oracle
Oracle Cloudは、最先端の機能をSoftware as a Service、Platform as a ServiceおよびInfrastructure as a ServiceおよびData as a Serviceとして提供します。

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
1
Help us understand the problem. What is going on with this article?