简体   繁体   English

Python 3,无法将日期时间戳转换为日期对象,TypeError:必需为整数(类型为str)

[英]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: 执行以下操作,然后查看:

  1. 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.

相关问题 类型错误:尝试将字典中的日期时间对象转换为字符串时需要整数(获取类型 datetime.datetime) - TypeError: an integer is required (got type datetime.datetime) when trying to convert a datetime object within a dictionary to a string Python - 类型错误:需要一个 integer(获取类型 datetime.datetime) - Python - TypeError: an integer is required (got type datetime.datetime) 类型错误:需要一个 integer(获取类型元组)日期时间 Python - TypeError: an integer is required (got type tuple) datetime Python Python:类型错误:需要一个整数(得到类型 str) - Python: TypeError: an integer is required (got type str) TypeError:Python中需要一个整数(类型为str) - TypeError: an integer is required (got type str) in python Python:TypeError 需要 integer(获取类型 str) - Python: TypeError an integer is required(got type str) python / flask:TypeError:需要一个整数(得到类型str) - python/flask: TypeError: an integer is required (got type str) Python Flask-TypeError:必须为整数(类型为str) - Python Flask - TypeError: an integer is required (got type str) 类型错误:在 Python 中打开文件时需要 integer(获取类型 str) - TypeError: an integer is required (got type str) when opening file in Python Python 2-如何修复TypeError:必须为整数(类型为str) - Python 2 - How to fix TypeError: an integer is required (got type str)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM