【问题标题】:Python - How to get a column 'duration' with start and end time column in csv?Python - 如何在csv中获取包含开始和结束时间列的“持续时间”列?
【发布时间】:2017-04-07 12:15:42
【问题描述】:

我在处理 csv 文件时遇到问题。 在我的 csv 文件中,有一列“开始时间”和“完成时间”,我想使用它来获取“持续时间”列。 当我尝试duration = row[2] - row[1] 时,它显示了一条错误消息

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

【问题讨论】:

标签: python csv duration


【解决方案1】:

由于你从csv文件中获取的数据是str类型的,所以不能进行操作:

duration = row[2] - row[1]

我建议你先转换row[2]和row[1]的内容,然后再做操作。

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

【讨论】:

    猜你喜欢
    • 2019-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-05
    • 2010-11-22
    • 1970-01-01
    • 1970-01-01
    • 2014-05-01
    相关资源
    最近更新 更多