python:locale.Error:不支持的语言环境设置

时间:2017-10-16 11:01:58

标签: python ubuntu utf-8 locale

我的代码有:

index type blah
1     0    abc
2     0    abc
3     2    ghi
4     1    def

因为我收到了这个错误,我试图运行它来测试:

def sep(num, lang='en', none_is_zero=False):
    if num is None:
        if none_is_zero is False:
            return None
        else:
            return 0
    try:
        locale.setlocale(locale.LC_ALL, lang)
    except locale.Error:
        locale.setlocale(locale.LC_ALL, 'en')
    return locale.format("%d", num, grouping=True)

我收到此错误:

def test(bot, job):
    print(sep(12343, 'it'))
    print(sep(11425, 'en'))
    print(sep(1234235, None))
    print(sep(42345, ''))
    print(sep(2353636, 'gergreh'))
    print(sep(None, None))

但是我没有在Windows上出现任何错误,并且该功能按预期工作。问题出在我的ubuntu vps上。 在stackoverflow上找到一些答案我看到有人建议使用' en_US.UTF-8'我试过,它甚至在Linux上工作。但是' en.UTF-8'仍然没有工作。我可以追加Traceback (most recent call last): File "/home/**/utils.py", line 192, in sep locale.setlocale(locale.LC_ALL, lang) File "/usr/lib/python3.5/locale.py", line 594, in setlocale return _setlocale(category, locale) locale.Error: unsupported locale setting During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/usr/local/lib/python3.5/dist-packages/telegram/ext/jobqueue.py", line 286, in tick job.run(self.bot) File "/usr/local/lib/python3.5/dist-packages/telegram/ext/jobqueue.py", line 411, in run self.callback(bot, self) File "/home/**/utils.py", line 199, in test print(sep(12343, 'it')) File "/home/**/utils.py", line 194, in sep locale.setlocale(locale.LC_ALL, 'en') File "/usr/lib/python3.5/locale.py", line 594, in setlocale return _setlocale(category, locale) locale.Error: unsupported locale setting 如果需要,但我使用的api有时只返回lang_code,' en','它'' es&#39 ;

我怎样摆脱这个问题?在Windows上我没有问题,似乎问题只是我的linux vps与UTF-8it

1 个答案:

答案 0 :(得分:0)

export LC_ALL="en_US.UTF-8"
export LC_CTYPE="en_US.UTF-8"
sudo dpkg-reconfigure locales