python - python 解析器库中的月份和日期交换

标签 python date parsing

我需要在python3中执行以下日期类型转换:

“2017 年 6 月 11 日 11:16:00 下午” -->“2017-11-06 23:16:00” 我尝试过几个库,但总是收到错误,即使解析器似乎正在工作

from dateutil import parser
dt = parser.parse("6/11/2017 11:16:00 p.m.")
print(dt)

输出:

2017-06-11 23:16:00

它交换日期和月份:(

提前致谢,

最佳答案

使用dayfirst参数 -

:param dayfirst:
Whether to interpret the first value in an ambiguous 3-integer date (e.g. 01/05/09) as the day (True) or month (False). If yearfirst is set to True, this distinguishes between YDM and YMD. If set to None, this value is retrieved from the current :class:parserinfo object (which itself defaults to False).

dt = parser.parse("6/11/2017 11:16:00 p.m.", dayfirst=True)

dt
2017-11-06 23:16:00

请参阅dateutil.parser.parse文档供引用。

关于python - python 解析器库中的月份和日期交换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47839339/

相关文章:

python - Kivy 应用程序在屏幕更改时退出

python - 通过 Http 将视频文件从 Unity 上传到 Python

python - 我可以将 curio 与 sanic 一起使用吗?为什么不?

python - 根据 pandas 的模式切换两列

python - 在 pandas 中使用 shift 和 rolling 与 groupBy

MYSQL - 即将到来的 X 出生日期

php - jQuery fullCalendar dayClick - 将(日期)转换为 mm/dd/yy

java - 将日历转换为 java.util.Date 时出现问题

python - 正则表达式/python 查找和替换字符串中的特定数字

python - 如何逐行读取并从python中的文件中解析?