如何使用googletrans在Python中翻译Pandas系列?

我希望将印度尼西亚语的pandas列翻译为英文,并将此翻译文本添加为数据框中名为“ English”的新列。这是我的代码:


from googletrans import Translator


translator = Translator()

df['English'] = translator.translate(df['Review to Translate'], src='id', dest='en')

但是,我收到此错误:


---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

<ipython-input-81-0fd41a244785> in <module>()

      2 

      3 translator = Translator()

----> 4 y['Review in English'] = translator.translate(y['Review to Translate'], src='id', dest='en')


~/anaconda3/lib/python3.6/site-packages/googletrans/client.py in translate(self, text, dest, src)

    170 

    171         origin = text

--> 172         data = self._translate(text, dest, src)

    173 

    174         # this code will be updated when the format is changed.


~/anaconda3/lib/python3.6/site-packages/googletrans/client.py in _translate(self, text, dest, src)

     73             text = text.decode('utf-8')

     74 

---> 75         token = self.token_acquirer.do(text)

     76         params = utils.build_params(query=text, src=src, dest=dest,

     77                                     token=token)


~/anaconda3/lib/python3.6/site-packages/googletrans/gtoken.py in do(self, text)

    179     def do(self, text):

    180         self._update()

--> 181         tk = self.acquire(text)

    182         return tk


~/anaconda3/lib/python3.6/site-packages/googletrans/gtoken.py in acquire(self, text)

    145         size = len(text)

    146         for i, char in enumerate(text):

--> 147             l = ord(char)

    148             # just append if l is less than 128(ascii: DEL)

    149             if l < 128:


TypeError: ord() expected a character, but string of length 516 found

有谁知道我该如何解决这个问题?我有一个很大的熊猫df。


汪汪一只猫
浏览 179回答 1
1回答
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python