gpt4 book ai didi

python - 使用 Strptime 时出现 ValueError

转载 作者:太空宇宙 更新时间:2023-11-03 14:09:35 25 4
gpt4 key购买 nike

我正在尝试从 starttime 中提取月份、小时和星期几。

City: NYC
OrderedDict([('tripduration', '839'),
('starttime', '1/1/2016 00:09:55'),
('stoptime', '1/1/2016 00:23:54'),
('start station id', '532'),
('start station name', 'S 5 Pl & S 4 St'),
('start station latitude', '40.710451'),
('start station longitude', '-73.960876'),
('end station id', '401'),
('end station name', 'Allen St & Rivington St'),
('end station latitude', '40.72019576'),
('end station longitude', '-73.98997825'),
('bikeid', '17109'),
('usertype', 'Customer'),
('birth year', ''),
('gender', '0')])
datum_n_m = example_trips['NYC'][datetime.strptime('starttime','%m/%d/%Y %H:%M:%S')]

ValueError: time data 'starttime' does not match format '%m/%d/%Y %H:%M:%S'

最佳答案

首先,让我们制作一个[mcve]

# you do need to import stuff
from datetime import datetime

# the ordered dict is irrelevant, so get rid of it

# this reproduces the error
print (datetime.strptime('starttime','%m/%d/%Y %H:%M:%S'))

这再现了

builtins.ValueError: time data 'starttime' does not match format '%m/%d/%Y %H:%M:%S'

这是因为字符串“starttime”不是该格式的时间。这只是几个字母。如果你做了一个基本的[mcve],你就会解决你自己的问题。基本调试:)

也许你的意思是:

start_time_as_string = example_trips['NYC']['starttime']
datum_n_m = datetime.strptime( start_time_as_string )

关于python - 使用 Strptime 时出现 ValueError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48599191/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com