[英]How to reformat date from 26-MAY-20 03.43.48.861000000 to 'yyyy-mm-dd hh:mm:ss'?
How would I convert '26-MAY-20 03.43.48.861000000'
into 'yyyy-mm-dd hh:mm:ss'
using datetime
in Python?如何使用
datetime
中的日期时间将'26-MAY-20 03.43.48.861000000'
转换为'yyyy-mm-dd hh:mm:ss'
?
So big picture, the goal is to create a date object out of the first string, and then format the date into the second string.这么大的图,目标是从第一个字符串中创建一个日期 object,然后将日期格式化为第二个字符串。
datetime.strptime will be your best friend here, along with datetime.strftime . datetime.strptime将与datetime.strftime一起成为您最好的朋友。
By referencing the library's format codes , we can put together a format of the first string.通过引用库的格式代码,我们可以将第一个字符串的格式放在一起。
from datetime import datetime
# I don't know why your datetime doesn't conform to the 1989 C standard but I'm just truncating it
may_26 = datetime.strptime('26-MAY-20 03.43.48.861000000'[:-3], "%d-%b-%y %H.%M.%S.%f")
output = datetime.strftime(may_26, "%Y-%m-%d %H:%M:%S")
I am making a few assumptions for you in the formatting, like assuming your datetime is on 24hr format, and stuff, so definitely check for yourself.我在格式上为你做了一些假设,比如假设你的日期时间是 24 小时格式,等等,所以一定要自己检查。
pandas.to_datetime
would also parse the nanoseconds (I've modified the timestring to have some...): pandas.to_datetime
也会解析纳秒(我已经修改了时间字符串以具有一些......):
import pandas as pd
t = pd.to_datetime('26-MAY-20 03.43.48.861000100', format="%d-%b-%y %H.%M.%S.%f")
t
Out[41]: Timestamp('2020-05-26 03:43:48.861000100')
t.microsecond
Out[42]: 861000
t.nanosecond
Out[43]: 100
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.