Python - 如何在 csv 中获取包含开始和结束时间列的列 'duration'?

Python - How to get a column 'duration' with start and end time column in csv?

我在处理 csv 文件时遇到问题。 在我的 csv 文件中,有一列 'Start Time' 和 'Complete Time',我想使用它来获得一列 'Duration'。 当我尝试 duration = row[2] - row[1] 时,它显示了一条错误消息

'unsupported operand type(s) for -: 'str' and 'str'.

由于您从csv文件中获取的数据为str类型,无法进行操作:

duration = row[2] - row[1]

建议你先转换行[2]和行[1]的内容,再进行运算

import time
complete_time = time.strptime(row[2])
start_time = time.strptime(row[1])
duration = complete_time - start_time