[英]How can I use gettext in jupyter-notebook/python interactive mode?
我使用以下代碼(從該問題中獲得)從其俄語名稱中獲取國家/地區代碼。
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中執行此代碼?
我找到了解決問題的方法。 install()函數在內置變量中設置變量_,如下所示:
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_code
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.