import pandas as pd
from googletrans import Translator
d = {"City_trad_chinese":["香港特别行政区",
"澳门特别行政区",
"北京市",
"上海市"]}
df = pd.DataFrame(data=d)
translator = Translator()
df["City_English"] = df["City_trad_chinese"].map(lambda x: translator.translate(x, src="zh-TW", dest="en").text)
我从StackOverflow_link中获得了以下代码段:
我想在代码段之后创建它:
print(df["City_English"])
0 Hong Kong Special Administrative Region
1 Macao Special Administrative Region
2 Beijing City
3 Shanghai City
但是当我尝试它时,会出现相同的错误:
Traceback (most recent call last):
File "<ipython-input-30-d5e66e13e679>", line 1, in <module>
df["City_English"] = df["City_trad_chinese"].map(lambda x: translator.translate(x, src="zh-TW", dest="en").text)
File "C:\Users\xxxx\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\series.py", line 2996, in map
arg, na_action=na_action)
File "C:\Users\xxxx\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\base.py", line 1004, in _map_values
new_values = map_f(values, mapper)
File "pandas/_libs/src\inference.pyx", line 1472, in pandas._libs.lib.map_infer
File "<ipython-input-30-d5e66e13e679>", line 1, in <lambda>
df["City_English"] = df["City_trad_chinese"].map(lambda x: translator.translate(x, src="zh-TW", dest="en").text)
File "C:\Users\xxxx\AppData\Local\Continuum\anaconda3\lib\site-packages\googletrans\client.py", line 172, in translate
data = self._translate(text, dest, src)
File "C:\Users\xxxx\AppData\Local\Continuum\anaconda3\lib\site-packages\googletrans\client.py", line 75, in _translate
token = self.token_acquirer.do(text)
File "C:\Users\xxxx\AppData\Local\Continuum\anaconda3\lib\site-packages\googletrans\gtoken.py", line 180, in do
self._update()
File "C:\Users\xxxx\AppData\Local\Continuum\anaconda3\lib\site-packages\googletrans\gtoken.py", line 59, in _update
code = unicode(self.RE_TKK.search(r.text).group(1)).replace('var ', '')
AttributeError: 'NoneType' object has no attribute 'group'
如何解决以上错误?
[注意]:
系统信息: