Python ValueError:保留未转换的数据:

Lui*_*uis 3 python time

我有以下代码:

这是我的代码:

print (start_timestamp)
start_timestamp_no_iso = datetime.strptime(start_timestamp, "%Y-%m-%dT%H:%M:%S.%f")
Run Code Online (Sandbox Code Playgroud)

这是我得到的:

INFO - 2018-11-20T14:44:03.452131
INFO - Traceback (most recent call last):
INFO - File "/home/ubuntu/script.py", line 84, in <module>
INFO - start_timestamp_no_iso = datetime.strptime(start_timestamp, "%Y-%m-%dT%H:%M:%S.%f")
INFO - File "/usr/lib/python3.6/_strptime.py", line 565, in _strptime_datetime
INFO - tt, fraction = _strptime(data_string, format)
INFO - File "/usr/lib/python3.6/_strptime.py", line 365, in _strptime
INFO - data_string[found.end():])
INFO - ValueError: unconverted data remains:
INFO - Command exited with return code 1
Run Code Online (Sandbox Code Playgroud)

我了解这意味着什么,但我不明白为什么会发生。我只是将时间戳从iso格式转换为non iso。问题是什么?

gra*_*pes 7

您的格式字符串%Y-%m-%dT%H:%M:%S.%f绝对正确。我敢肯定,您已经line feeds排到了最后。尝试

start_timestamp_no_iso = datetime.strptime(start_timestamp.strip(' \t\r\n'), "%Y-%m-%dT%H:%M:%S.%f")
Run Code Online (Sandbox Code Playgroud)

  • 仅仅是`start_timestamp.strip()`就足够了吗?(使用默认剥离行为) (2认同)