Python 3 , Unable to convert datetime stamp to datetime object, TypeError: an integer is required (got type str)
Python 3 , Unable to convert datetime stamp to datetime object, TypeError: an integer is required (got type str)
我正在尝试处理 CSV 格式的时间序列数据并计算时间纪元的数量,对于此示例,纪元 = 1000 毫秒
这里是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')
我 运行 通过 Spyder IDE 并使用 Python 3.6.2
我的输入:
get_number_of_epochs(1000)
数据示例:
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
错误:
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)
如何在我的代码中使用当前 API 修复此错误?
我想你要找的是
datetime.datetime.strptime(datetime_str, '%Y-%m-%d %H:%M:%S:%f')
标准 datetime
构造函数看起来像
datetime.datetime(year: int, month: int, day: int, hour: int, minute: int, second: int, millisecond: int, tzinfo: datetime.tzinfo)
执行以下操作并查看:
将您的 str_to_datetime_ 函数更改为以下内容
def str_to_datetime_(datetime_str):
return datetime.datetime.strptime(datetime_str, '%Y-%m-%d %H:%M:%S:%f')
我正在尝试处理 CSV 格式的时间序列数据并计算时间纪元的数量,对于此示例,纪元 = 1000 毫秒
这里是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')
我 运行 通过 Spyder IDE 并使用 Python 3.6.2
我的输入:
get_number_of_epochs(1000)
数据示例:
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
错误:
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)
如何在我的代码中使用当前 API 修复此错误?
我想你要找的是
datetime.datetime.strptime(datetime_str, '%Y-%m-%d %H:%M:%S:%f')
标准 datetime
构造函数看起来像
datetime.datetime(year: int, month: int, day: int, hour: int, minute: int, second: int, millisecond: int, tzinfo: datetime.tzinfo)
执行以下操作并查看:
将您的 str_to_datetime_ 函数更改为以下内容
def str_to_datetime_(datetime_str): return datetime.datetime.strptime(datetime_str, '%Y-%m-%d %H:%M:%S:%f')