python - 将 csv 中的数据和时间列转换为相对时间

标签 python csv datetime python-3.x data-analysis

我有一个 csv 文件,其中有两列,其中包含日期和时间。整个时间段为 24 小时。我想将这两列转换为从 00:00:00 - 23:59:59 开始的相对时间的单列。

Layer      date       time        Ht    Stat      Vg     Temp
57986   8/01/2015   13:53:05    0.00m    87      15.4    None
20729   8/01/2015   11:23:21    45.06m   82      11.6    None
20729   8/01/2015   11:44:36    45.06m   81      11.6    None
20729   8/01/2015   12:17:11    46.08m   79      11.6    None

示例 csv 数据如上所示。

 with open('output_file.csv','rb') as inf:
        incsv = csv.reader(inf)
        row = next(incsv)
        for row in incsv:
            date.append(row[1])
            time.append(row[2])
        print('min date {} max date {} min time {} max time {}'.format(min(date),max(date),min(time),max(time)))

我有日期和时间列的最小值和最大值。我想将这两列转换为相对时间列,其中包含从 00:00:00 - xx:xx:xx 开始的相对时间

我该怎么做?

最佳答案

对于名为 output_file.csv 的 CSV 输入文件:

Layer,      date,       time,        Ht,    Stat,      Vg,     Temp
57986,   8/01/2015,   13:53:05,    0.00m,    87,      15.4,    None
20729,   8/01/2015,   11:23:21,    45.06m,   82,      11.6,    None
20729,   8/01/2015,   11:44:36,    45.06m,   81,      11.6,    None
20729,   8/01/2015,   12:17:11,    46.08m,   79,      11.6,    None

这个程序:

import csv
import datetime

min_date = None

row_list = []
date_list = []

with open('output_file.csv', 'rb') as inf:
    incsv = csv.reader(inf)
    row = next(incsv)
    for row in incsv:
        # Create a time string
        time_str = row[1].strip() + " " + row[2].strip()
        # Convert the time string to a datetime
        cur_date = datetime.datetime.strptime(time_str, "%m/%d/%Y %H:%M:%S")
        # Update the min_date
        if min_date is None:
            min_date = cur_date
        else:
            min_date = min(min_date, cur_date)
        # Append the datetime to the list
        date_list.append(cur_date)
        # Get a copy of the row with whitespace removed
        new_row = [ col.strip() for col in row]
        # Get a copy of the row with the date and time replaced by a 
        # placeholder
        new_row = [ new_row[0], "" ] + new_row[3:]
        # Append the row to the list
        row_list.append(new_row)

index = 0
# For each datetime in the list
for cur_date in date_list:
    # Calculate the time delta
    delta_date = cur_date - min_date
    # Store it in the placeholder
    row_list[index][1] = str(delta_date)
    index += 1

for row in row_list:
    print "%s" % row

产生此输出:

['57986', '2:29:44', '0.00m', '87', '15.4', 'None']
['20729', '0:00:00', '45.06m', '82', '11.6', 'None']
['20729', '0:21:15', '45.06m', '81', '11.6', 'None']
['20729', '0:53:50', '46.08m', '79', '11.6', 'None']

您可能需要修改它才能准确地生成您想要的内容。

关于python - 将 csv 中的数据和时间列转换为相对时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33764999/

相关文章:

vb.net - 将当前日期加一个月

PHP 确定提供的日期中的下一个是星期一还是星期四

python - 在 Pandas 中操作子集数据帧的行值

python - 如何将 R^2 值添加到 seaborn 条形图的图例中?

python - Exchangelib:将电子邮件从收件箱移动到文件夹

c - 在枚举中包含 .csv 如何工作?

python - 包含方括号的搜索模式

python - 属性错误: 'list' object has no attribute 'split' when i try to split a row from csv file

sql - 写数据流到postgresql

c# - 如何使用 NodaTime 将 IANA zone + datetime 转换为 UTC + Offset