2017-11-15 19 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