gpt4 book ai didi

python - 是否可以将语言环境重置为可移植语言环境?

转载 作者:太空宇宙 更新时间:2023-11-04 08:21:38 28 4
gpt4 key购买 nike

根据 Python 文档:

"a program which has not called setlocale(LC_ALL, '') runs using the portable 'C' locale.

在使用 setlocale(LC_ALL, '') 设置语言环境后,是否可以将语言环境重置回“便携”状态?我在一个库中工作,其中包含一些行为不端的组件,这些组件试图将语言环境全局重置为特定区域 - 我需要找到一种方法将语言环境恢复为可移植状态。

import locale
loc = locale.getlocale(locale.LC_ALL) # get current locale
assert loc == (None, None)
# Locale is unset, therefore in the "portable" state.
locale.setlocale(locale.LC_ALL, '') # use user's preferred locale
# Loc is not set to regional default
???? DO SOMETHING HERE
assert loc == locale.getlocale(locale.LC_ALL) # I want to make this true!

需要在 Windows XP 32 位上使用 Python 2.4.4

最佳答案

您可以尝试:locale.setlocale(locale.LC_ALL, loc)

>>> locale.getlocale(locale.LC_ALL)
(None, None)
>>> locale.setlocale(locale.LC_ALL, "")
'en_US.utf8'
>>> locale.getlocale(locale.LC_ALL)
('en_US', 'UTF8')
>>> locale.setlocale(locale.LC_ALL, "C")
'C'
>>> locale.getlocale(locale.LC_ALL)
(None, None)
>>> locale.setlocale(locale.LC_ALL, (None,None))
'C'
>>> locale.getlocale(locale.LC_ALL)
(None, None)

关于python - 是否可以将语言环境重置为可移植语言环境?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4305410/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com