为了账号安全,请及时绑定邮箱和手机立即绑定

使用 csv 模块和 datetime 模块解析时间戳

使用 csv 模块和 datetime 模块解析时间戳

泛舟湖上清波郎朗 2023-05-23 16:32:24
我datetime在使用 Python 中的模块时遇到了一些问题。我有来自 csv 文件的数据:user_id,timestamp563,0:00:21671,0:00:26780,0:00:28这是我的代码:import csvfrom datetime import datetimepath = "/home/haldrik/dev/python/data/dataset.csv"file = open(path, newline='')reader = csv.reader(file, delimiter=',')header = next(reader) # Ignore first row.data = []for row in reader:    # row = [user_id, timestamp]    user_id = row[0]    timestamp = datetime.strptime(row[1], '%H:%M:%S').time()        data.append([user_id, timestamp])该代码抛出此错误:Traceback (most recent call last):  File "/home/haldrik/dev/python/instances_web_site.py", line 15, in <module>    date = datetime.strptime(row[1], '%H:%M:%S').time()  File "/usr/lib/python3.8/_strptime.py", line 568, in _strptime_datetime    tt, fraction, gmtoff_fraction = _strptime(data_string, format)  File "/usr/lib/python3.8/_strptime.py", line 349, in _strptime    raise ValueError("time data %r does not match format %r" %ValueError: time data '' does not match format '%H:%M:%S'我找不到错误在哪里。我可以看到数据格式符合指定的时间格式。倒出 cvs 导入步骤,我可以确保它有效,请参见这段代码(未包含在上面的代码中):data_import = [row for row in reader]print(data_import[0])它输出这个:['563','0:00:21']
查看完整描述

1 回答

?
梦里花落0921

TA贡献1772条经验 获得超6个赞

您对 timestamp 列中的一个或多个值有疑问,其中一行看起来像440,并且会导致time data '' does not match format '%H:%M:%S'

包裹date = datetime.strptime(row[1], '%H:%M:%S').time()在一个try-except块中。

test.csv

user_id,timestamp

563,0:00:21

671,0:00:26

780,0:00:28

440,

代码

import csv

from datetime import datetime


path = "test.csv"

file = open(path, newline='')


reader = csv.reader(file, delimiter=',')


header = next(reader) # Ignore first row.


data = []

for row in reader:

    # row = [user_id, timestamp]

    user_id = row[0]

    try:

        timestamp = datetime.strptime(row[1], '%H:%M:%S').time()

    except ValueError as e:

        timestamp = row[1]

#         continue  # use this if you do not want the row added to data, comment it out otherwise

    

    data.append([user_id, timestamp])



print(data)

[out]:

[['563', datetime.time(0, 0, 21)], ['671', datetime.time(0, 0, 26)], ['780', datetime.time(0, 0, 28)], ['440', '']]



查看完整回答
反对 回复 2023-05-23
  • 1 回答
  • 0 关注
  • 148 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信