Python 3,无法将datentime标记转换为datetime对象,TypeError:需要一个整数(获取类型str)

时间:2017-12-08 07:40:22

标签: python python-datetime

我试图用CSV处理时间序列数据并计算时间纪元数,本例中epoch = 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

错误:

文件“”,第33行,在get_number_of_epochs中     initial_datetime = str_to_datetime_(row [1])

文件“”,第45行,在str_to_datetime_中     return datetime.datetime(datetime_str,'%Y-%m-%d%H:%M:%S:%f')

TypeError:需要一个整数(得到str类型)

如何使用代码中的当前API修复此错误?

由于

2 个答案:

答案 0 :(得分:2)

执行以下操作并查看:

  1. str_to_datetime _ 功能更改为以下

    def  str_to_datetime_(datetime_str):
        return datetime.datetime.strptime(datetime_str, '%Y-%m-%d %H:%M:%S:%f')
    

答案 1 :(得分:0)

我认为你在寻找的是

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)