[英]Date format conversion works on windows but gives an error in Linux
I am trying to format date to a specific format.我正在尝试将日期格式化为特定格式。 The code deploys successfully on Windows with Python 3.7.
该代码使用 Python 3.7 在 Windows 上成功部署。 however, it doesn't work on Linux Debian 9.11 - Oython 3.5.
但是,它不适用于 Linux Debian 9.11 - Oython 3.5。 Cannot figure out the solution.
想不出解决办法。 Any help is really appreciated.
非常感谢任何帮助。
def parse_date(date_string: str, date_format: str) -> str:
"""
'2019-04-12T00:00:00.000-07:00' --> "%Y-%m-%dT%H:%M:%S.%f%z"
'2019-04-28T07:25:39.668Z' --> "%Y-%m-%dT%H:%M:%S.%fZ"
"""
req_date = dt.datetime.strptime(date_string, date_format)
return req_date.strftime("%Y-%m-%d")
Works on windows适用于 windows
parse_date('2019-04-11T00:00:00.000-07:00', "%Y-%m-%dT%H:%M:%S.%f%z")
Fails on Linux在 Linux 上失败
parse_date('2019-04-11T00:00:00.000-07:00', "%Y-%m-%dT%H:%M:%S.%f%z")
ValueError: time data '2019-04-11T00:00:00.000-07:00' does not match format '%Y-%m-%dT%H:%M:%S.%f%z'
ValueError:时间数据“2019-04-11T00:00:00.000-07:00”与格式“%Y-%m-%dT%H:%M:%S.%f%z”不匹配
expected return value: '2019-04-11'
预期返回值:
'2019-04-11'
I ended up using regex to solve overcome the problem and it works.我最终使用正则表达式解决了这个问题并且它有效。
def parse_date_regex(date_string: str) -> str:
"""
:return:
"""
date = re.split('(\d{4}-\d{2}-\d{2})', date_string)
return [res for res in date if len(res) == 10][0]
In Python 3.5, you could not have a colon in your UTC offset.在 Python 3.5 中,您的 UTC 偏移量中不能有冒号。
UTC offset in the form +HHMM or -HHMM (empty string if the object is naive).
格式为 +HHMM 或 -HHMM 的 UTC 偏移量(如果 object 是幼稚的,则为空字符串)。
In Python 3.7 that changed to allow the colon.在 Python 3.7 中更改为允许冒号。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.