简体   繁体   English

python 字符串转时间格式错误~

[英]format error in python string to time conversion~

Currently, test 32 is the str type.目前,test 32 是 str 类型。 I would like to express the str type of test32 in time data.我想用时间数据来表达test32的str类型。 yyyy-mm-dd hh:mm:ss method... However, time data '22/03/0823:33:55.256' does not match format '%Y%m%d%H%M%S' error occurs. yyyy-mm-dd hh:mm:ss 方法...但是,时间数据 '22/03/0823:33:55.256' does not match format '%Y%m%d%H%M%S' 错误发生。 Is there any way?有什么办法吗?

    for z in data:
        if 'D:\System\iUTILITY\Tool\Curver\ToolBox\STG\i02-K01_S1_CEC_Update_Monintor_Analysis.xpsp' in z:
            test30 = z.split(' ')[0:2]
            test31 = ''.join(test30)
            
            test32 = test31.split(',')[0]
            print(type(test32))
            test33 = datetime.datetime.strptime(test32, '%Y%m%d%H%M%S')
            print(test33)
            # print(test32)

To parse the date 22/03/0823:33:55.256 , you need to use format %y/%m/%d%H:%M:%S.%f , like this:要解析日期22/03/0823:33:55.256 ,您需要使用格式%y/%m/%d%H:%M:%S.%f ,如下所示:

datetime.datetime.strptime('22/03/0823:33:55.256', '%y/%m/%d%H:%M:%S.%f')
# Ouptut: datetime.datetime(2022, 3, 8, 23, 33, 55, 256000)

According to the document, %y stands for the year without century, and %f stands for the microsecond.根据文档, %y代表没有世纪的年份, %f代表微秒。 Here's the document:这是文档:

https://docs.python.org/3/library/datetime.html#strftime-and-strptime-format-codes https://docs.python.org/3/library/datetime.html#strftime-and-strptime-format-codes

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM