简体   繁体   中英

np.loadtxt datetime error: data ValueError: time data ' 2018-10-01 11:29:40.475195' does not match format '%Y-%m-%d %H:%M:%S.%f'

I have a .txt file with following data:

2018-10-01 11:29:31.147695, -1.006520e-01, 2018-10-01 11:29:40.475195, 1.021820e-01, 2018-10-01 11:29:42.835195, -2.164020e-01

now I want to load the .txt with np.loadtxt . I have:

convertfunc =lambda x : dt.datetime.strptime(x.decode("ascii"), '%Y-%m-%d %H:%M:%S.%f')
min_max = np.loadtxt("file.txt", delimiter=',', converters={0: convertfunc, 2: convertfunc, 4: convertfunc})

following error occurs:

convertfunc =lambda x : dt.datetime.strptime(x.decode("ascii"), '%Y-%m-%d %H:%M:%S.%f')
min_max = np.loadtxt(r"C:\Users\mdk\Desktop\93307\location_1.txt", delimiter=',', converters={0: convertfunc, 2: convertfunc, 4: convertfunc})

can somebody help me with a solution?

There is an extra whitespace in front of your date string. Note in the error message:

' 2018-10-01 11:29:40.475195'

This happens for all but the first entry, because your delimiter is actually ,<space> , not , .

I am currently not able to test it, but you should have two options:

  1. Strip whitespace before parsing:

     convertfunc = lambda x : dt.datetime.strptime(x.decode("ascii").strip(), '%Y-%m-%d %H:%M:%S.%f') 
  2. Use ', ' as delimiter instead of ','

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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