Skip to main content
 首页 » 编程设计

python之在 python 中转换 strptime 中的数据时出现问题

2024年12月31日10freeliver54

我正在尝试计算从一个点到另一个点的平均时间距离。这是导入数据的函数。

处理时间时,开始时间的类型列表有误

时间记录:

[['01/01/2016 00h15m'], ['01/01/2016 00h48m'], ['01/01/2016 01h08m'], ['01/01/2016 03h06m'], ['01/01/2016 05h22m'], ['01/01/2016 08h34m'], ['01/01/2016 09h59m']] 

函数 :

for x in stime_record: 
    datetime.strptime(str(x), '%d/%m/%Y %Hh%Mm') 
 
ValueError: time data "['01/01/2016 00h15m']" does not match format '%d/%m/%Y %Hh%Mm' 

请您参考如下方法:

简单地试试这个:

datetime.strptime(str(x[0]), '%d/%m/%Y %Hh%Mm') 

这就是为什么你有一个列表的列表,所以在每次迭代中 x 将是一个列表而不是字符串,当你将它转换为 str 时,它不是去匹配。