0

Currently, test 32 is the str type. I would like to express the str type of test32 in time data. 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. 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)
Mandyz
  • 7
  • 3
  • you could use `try: convert with one format` `except: convert with another format` –  Mar 21 '22 at 07:32

1 Answers1

0

To parse the date 22/03/0823:33:55.256, you need to use format %y/%m/%d%H:%M:%S.%f, like this:

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. Here's the document:

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

Zheng Bowen
  • 339
  • 2
  • 7