我使用从this question获得的以下代码从他们的俄罗斯名字中获取国家代码。
def get_country_code(russian_name):
gettext.translation('iso3166', pycountry.LOCALES_DIR, languages=['RU']).install()
country_code = ''.join([country.alpha_2 for country in pycountry.countries if _(country.name) == russian_name])
return country_code但是当我尝试在我的jupyter-notebook中使用这个函数时,它给出了以下错误:
TypeError: 'str' object is not callable这似乎是因为在jupyter中以及在交互模式下-意味着前一次计算的结果,但gettext将其定义为其函数。
如何在Jupyter中执行此代码?
发布于 2017-11-16 01:48:03
我已经找到了这个问题的解决方案。install()函数像这样设置variable _ in内置函数:
builtins.__dict__['_'] = self.gettext所以我们可以使用这个函数。
def get_country_code(russian_name):
rus = gettext.translation('iso3166', pycountry.LOCALES_DIR, languages=['RU'])
rus.install()
country_code = ''.join([country.alpha_2 for country in pycountry.countries if rus.gettext(country.name) == russian_name]) # u'FR'
return country_codehttps://stackoverflow.com/questions/47313585
复制相似问题