2017-11-15 13 views
0

私はthis questionから得た次のコードを使用して、ロシア語の名前から国コードを取得します。jupyter-notebook/python対話モードでgettextを使うにはどうすればいいですか?

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-ノートにこの機能を使用しようとして、それは私に次のエラーを与える:

であるが、このように思える
TypeError: 'str' object is not callable 

があるのでjupyter内だけでなく、対話モードで_前の計算の結果を意味しますが、gettextはそれをその関数として定義します。

Jupyterでこのコードを実行するにはどうすればよいですか?

答えて

0

私はこの問題の解決策を見つけました。 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 
関連する問題