我正在使用 datetime.strptime() 从一个月中获取月份编号,用全英文编写。我想将它与一些 PySide Qt 接口结合使用。但是,一旦我启动 QApplication,月份识别似乎就会中断。
最小工作示例(我正在打印语言环境以表明它不会改变):
import sys, datetime
from PySide.QtCore import *
from PySide.QtGui import *
import locale
print(locale.getlocale())
full_date = 'February'
print(datetime.datetime.strptime(full_date,'%B'))
输出:
('en_US', 'UTF-8')
1900-02-01 00:00:00
现在,当我启动 QApplication 并执行相同操作时:
import sys, datetime
from PySide.QtCore import *
from PySide.QtGui import *
import locale
qt_app = QApplication(sys.argv)
print(locale.getlocale())
full_date = 'February'
print(datetime.datetime.strptime(full_date,'%B'))
输出:
('en_US', 'UTF-8')
Traceback (most recent call last):
File "my_timedate.py", line 12, in <module>
print(datetime.datetime.strptime(full_date,'%B'))
File "/usr/lib/python3.5/_strptime.py", line 510, in _strptime_datetime
tt, fraction = _strptime(data_string, format)
File "/usr/lib/python3.5/_strptime.py", line 343, in _strptime
(data_string, format))
ValueError: time data 'February' does not match format '%B'
到底是怎么回事?为什么启动 QApplication 后它停止工作?