[英]Value Error: time data '12:00:01 AM' does not match format '%I:%M:00 %p' using time.strptime
I'm a bit new to python so any help is greatly appreciated. 我是python的新手,因此非常感谢您的帮助。 Thanks in advance (and sorry for any mislabel).
在此先感谢您(并感谢您贴错标签)。
I'm working on a csv file containing columns with Date, Time, CO, CO2 and CH4. 我正在处理一个包含日期,时间,CO,CO2和CH4列的csv文件。 What I want to achieve is to make a loop so that every time there is a time with zero seconds (ex: "12:00:00 AM", "3:05:00 PM" etc) it will take the data of that row and send it to a new text or csv file(this part is not included in the code).
我要实现的是做一个循环,以便每次有零秒的时间(例如:“ 12:00:00 AM”,“ 3:05:00 PM”等)都将获取该数据行并将其发送到新的文本或csv文件(此部分未包含在代码中)。 I imported the csv using pandas and used time.strptime to convert the string to readable time format.
我使用熊猫导入了csv,并使用time.strptime将字符串转换为可读的时间格式。
Unfortunately since there is some data missing, I can't make a loop to gather every 60th data for this. 不幸的是,由于缺少一些数据,因此无法循环收集每60个数据。 I've also tried making a function using strptime but it also gives me a type error saying it must be a string and not a panda core series.
我也尝试过使用strptime创建一个函数,但是它也给我一个类型错误,说它必须是字符串,而不是熊猫核心系列。
Importing the csv file: 导入csv文件:
data1 = pd.read_csv("prueba1.csv")
print(data1)
Where the output is: 输出为:
DATE TIME CO CO2_dry CH4_dry
0 3/4/2019 12:00:00 AM 0.352 420 1.99
1 3/4/2019 12:00:01 AM 0.352 420 1.99
2 3/4/2019 12:00:02 AM 0.352 420 1.99
3 3/4/2019 12:00:03 AM 0.366 420 1.99
4 3/4/2019 12:00:04 AM 0.366 420 1.99
5 3/4/2019 12:00:05 AM 0.366 421 1.99
6 3/4/2019 12:00:06 AM 0.369 421 1.99
7 3/4/2019 12:00:07 AM 0.369 421 1.99
8 3/4/2019 12:00:09 AM 0.354 421 1.99
9 3/4/2019 12:00:10 AM 0.354 421 1.99
And the code I'm using is 我正在使用的代码是
for i in data1["TIME"]:
time.strptime(i,"%I:%M:%S %p")
if time.strptime(i,"%I:%M:%S %p") == time.strptime(i,"%I:%M:00 %p"):
print("Found a number!", i)
else:
print("Yikes")
The error message is: 错误消息是:
Found a number! 12:00:00 AM
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-18-8b936d17df46> in <module>()
2 time.strptime(i,"%I:%M:%S %p")
3 #print(i)
----> 4 if time.strptime(i,"%I:%M:%S %p") == time.strptime(i,"%I:%M:00 %p"):
5 print("Found a number!", i)
6 else:
C:\Users\Diego\Anaconda3\lib\_strptime.py in _strptime_time(data_string, format)
557 """Return a time struct based on the input string and the
558 format string."""
--> 559 tt = _strptime(data_string, format)[0]
560 return time.struct_time(tt[:time._STRUCT_TM_ITEMS])
561
C:\Users\Diego\Anaconda3\lib\_strptime.py in _strptime(data_string, format)
360 if not found:
361 raise ValueError("time data %r does not match format %r" %
--> 362 (data_string, format))
363 if len(data_string) != found.end():
364 raise ValueError("unconverted data remains: %s" %
ValueError: time data '12:00:01 AM' does not match format '%I:%M:00 %p'
It returns the preceding output. 它返回前面的输出。 I expected for it to return all time numbers matching the '%I:%M:00 %p' format, but only returned the first number.
我希望它返回所有与'%I:%M:00%p'格式匹配的时间数字,但只返回第一个数字。 It seems odd to me that it stopped after it encountered the first number not matching the specified format.
我觉得奇怪的是,它遇到第一个与指定格式不匹配的数字后就停止了运行。
If you want to skip errors, then you should use try
and except
如果你想跳过错误,那么你应该使用
try
和except
for i in data1["TIME"]:
try:
time.strptime(i,"%I:%M:%S %p")
if time.strptime(i,"%I:%M:%S %p") == time.strptime(i,"%I:%M:00 %p"):
print("Found a number!", i)
else:
print("Yikes")
except ValueError:
print("Ouch! Something failed")
You are doing it the long way. 您正在做很长的路要走。
pd.to_datetime
is both fast and convenient: pd.to_datetime
既方便又快捷:
df['DATE'] = pd.to_datetime(df['DATE'])
df['TIME'] = pd.to_datetime(df['TIME']).dt.time
Of if your situation calls for a precise format: 如果您的情况需要精确的格式:
df['DATE'] = pd.to_datetime(df['DATE'], format='%m/%d/%Y')
df['TIME'] = pd.to_datetime(df['TIME'], format='%I:%M:%S %p').dt.time
You can get the datetime format specifiers from strftime.org 您可以从strftime.org获取日期时间格式说明符
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.