[英]Python 3 , Unable to convert datetime stamp to datetime object, TypeError: an integer is required (got type str)
Im trying to process timeseries data in CSV and count the number of time epochs, for this example epoch = 1000 millisecs 我正在尝试处理CSV中的时间序列数据并计算时间段数,在此示例中,时间段= 1000毫秒
Here is the python code: 这是python代码:
import csv
import os
import datetime
eeg_record = []
path = "C:\\Users\\ary\\Desktop\\nuerosky blink tests\\backup\\process_from_backup\\blink_50px\\task_blink40000radious50px_duration_40000.csv"
"""read CSV file"""
def get_dirs(path_):
if os.access(path_,os.F_OK and os.R_OK):
f = open(path_)
row_counter = 0
for row in csv.reader(f):
##print(row)
##skip the header
if row_counter == 0:
row_counter = row_counter + 1
continue
else:
eeg_record.append(row)
f.close()
"""get the number of epochs based on a epoch size in milliseconds"""
def get_number_of_epochs(epoch_size_in_milliseconds):
initial_datetime = datetime.datetime(2000,1,1,0,0,0,0)
current_datetime = datetime.datetime(2000,1,1,0,0,0,0)
epoch_counter = 0
row_counter = 0
epoch = datetime.timedelta(milliseconds=epoch_size_in_milliseconds)
for row in eeg_record:
if row_counter == 0:
row_counter = row_counter + 1
initial_datetime = str_to_datetime_(row[1])
else:
current_datetime = str_to_datetime_(row[1])
if initial_datetime - current_datetime >= epoch:
initial_datetime = current_datetime
epoch_counter = epoch_counter + 1
print("counter: ",epoch_counter)
""" covert datetime of this format 2017-10-13 19:22:50:525 to datetime object"""
def str_to_datetime_(datetime_str):
return datetime.datetime(datetime_str,'%Y-%m-%d %H:%M:%S:%f')
Im running this via Spyder IDE and using Python 3.6.2 我通过Spyder IDE并使用Python 3.6.2运行它
My input: 我的输入:
get_number_of_epochs(1000)
Example of the data: 数据示例:
Time_Stamp_In_Milli_Secs,Time_Stamp_Formatted,Raw,A,B,C,D,Class_Stimulia
1.50795E+12,2017-10-13 19:22:13:249,54,-1,0,0,-1,NO_STIMULIA
1.50795E+12,2017-10-13 19:22:13:249,55,-1,0,0,-1,NO_STIMULIA
1.50795E+12,2017-10-13 19:22:13:250,55,-1,0,0,-1,NO_STIMULIA
1.50795E+12,2017-10-13 19:22:13:250,54,-1,0,0,-1,NO_STIMULIA
1.50795E+12,2017-10-13 19:22:13:250,54,-1,0,0,-1,NO_STIMULIA
1.50795E+12,2017-10-13 19:22:13:250,54,-1,0,0,-1,NO_STIMULIA
1.50795E+12,2017-10-13 19:22:13:250,54,-1,0,0,-1,NO_STIMULIA
1.50795E+12,2017-10-13 19:22:13:251,54,-1,0,0,-1,NO_STIMULIA
1.50795E+12,2017-10-13 19:22:13:251,54,-1,0,0,-1,NO_STIMULIA
1.50795E+12,2017-10-13 19:22:13:251,54,-1,0,0,-1,NO_STIMULIA
1.50795E+12,2017-10-13 19:22:13:251,54,-1,0,0,-1,NO_STIMULIA
1.50795E+12,2017-10-13 19:22:13:251,54,-1,0,0,-1,NO_STIMULIA
1.50795E+12,2017-10-13 19:22:13:251,54,-1,0,0,-1,NO_STIMULIA
1.50795E+12,2017-10-13 19:22:13:252,54,-1,0,0,-1,NO_STIMULIA
1.50795E+12,2017-10-13 19:22:13:252,54,-1,0,0,-1,NO_STIMULIA
1.50795E+12,2017-10-13 19:22:13:252,54,-1,0,0,-1,NO_STIMULIA
1.50795E+12,2017-10-13 19:22:13:252,54,-1,0,0,-1,NO_STIMULIA
1.50795E+12,2017-10-13 19:22:13:252,55,-1,0,0,-1,NO_STIMULIA
1.50795E+12,2017-10-13 19:22:13:253,54,-1,0,0,-1,NO_STIMULIA
1.50795E+12,2017-10-13 19:22:13:253,54,-1,0,0,-1,NO_STIMULIA
1.50795E+12,2017-10-13 19:22:13:266,54,-1,0,0,-1,NO_STIMULIA
1.50795E+12,2017-10-13 19:22:13:266,54,-1,0,0,-1,NO_STIMULIA
1.50795E+12,2017-10-13 19:22:13:266,55,-1,0,0,-1,NO_STIMULIA
1.50795E+12,2017-10-13 19:22:13:267,54,-1,0,0,-1,NO_STIMULIA
1.50795E+12,2017-10-13 19:22:13:267,55,-1,0,0,-1,NO_STIMULIA
1.50795E+12,2017-10-13 19:22:13:267,54,-1,0,0,-1,NO_STIMULIA
1.50795E+12,2017-10-13 19:22:13:267,54,-1,0,0,-1,NO_STIMULIA
1.50795E+12,2017-10-13 19:22:13:267,54,-1,0,0,-1,NO_STIMULIA
1.50795E+12,2017-10-13 19:22:13:268,54,-1,0,0,-1,NO_STIMULIA
1.50795E+12,2017-10-13 19:22:13:268,53,-1,0,0,-1,NO_STIMULIA
1.50795E+12,2017-10-13 19:22:13:280,54,-1,0,0,-1,NO_STIMULIA
1.50795E+12,2017-10-13 19:22:13:280,54,-1,0,0,-1,NO_STIMULIA
1.50795E+12,2017-10-13 19:22:13:280,54,-1,0,0,-1,NO_STIMULIA
The error: 错误:
File "<ipython-input-235-fac5a2a567a4>", line 33, in get_number_of_epochs
initial_datetime = str_to_datetime_(row[1])
File "<ipython-input-235-fac5a2a567a4>", line 45, in str_to_datetime_
return datetime.datetime(datetime_str,'%Y-%m-%d %H:%M:%S:%f')
TypeError: an integer is required (got type str)
How can I fix this error using the current APIs in my code? 如何使用代码中的当前API修复此错误?
Do the following and see: 执行以下操作,然后查看:
change your str_to_datetime_ function to the following 将您的str_to_datetime_函数更改为以下内容
def str_to_datetime_(datetime_str): return datetime.datetime.strptime(datetime_str, '%Y-%m-%d %H:%M:%S:%f')
I think what you are looking for is 我想你要找的是
datetime.datetime.strptime(datetime_str, '%Y-%m-%d %H:%M:%S:%f')
The standard datetime
constructor looks like 标准的datetime
构造函数看起来像
datetime.datetime(year: int, month: int, day: int, hour: int, minute: int, second: int, millisecond: int, tzinfo: datetime.tzinfo)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.