Home>

Thanks for your help.
I'm new to Python.
I am trying to save the contract information and the time information at that time in a list from the Bitflyer API in Python.
I would like to include the decimal time in the time information.
I don't know how to change the datetime.
For example, second = float (data [17:22]) will also result in an error.
Can anyone give me a hint?
What is the value of the date?
"2020-11-22T02: 18: 34.967"

import requests
from datetime import datetime, timedelta
import time

price_list = [] #Empty list creation
data_list = [] # Create an empty list
response = requests.get ("https://api.bitflyer.com/v1/getexecutions") #bitfyer API launch
response = response.json () #Json support
price = response [0] ["price"] # Get final price
date = response [0] ["exec_date"] #Get trading time
i = 0
while i>-101:
    price = response [int (i)] ["price"] # Get final price
    date = response [int (i)] ["exec_date"] #Get trading time
    utc_split = datetime (
        year = int (date [0: 4]), month = int (date [5: 7]), day = int (date [8:10]),
        hour = int (date [11:13]), minute = int (date [14:16]), second = int (date [17:19])
    )
    exec_data = utc_split + timedelta (hours = + 9) #time difference calculation
    price_list.append (price) #Display price list
    tstr = exec_data.strftime ('% Y-% m-% d% H:% M:% S') #Time data string conversion
    data_list.append (tstr) #Display time data
    i = i -1
print (price_list)
print (data_list)
Traceback (most recent call last):
File "bit_befor.py", line 17, in<module>
utc_split = datetime (
TypeError: integer argument expected, got float
  • Answer # 1

    Can't you use strptime?

    from datetime import datetime, timedelta
    date = '2020-11-22T13: 24: 07.13'
    dt = datetime.strptime (date,'% Y-% m-% dT% H:% M:% S.% f')
    print (dt) # 2020-11-22 13: 24: 07.130000

  • Answer # 2

    Values ​​smaller than seconds should be values ​​in microsecondsmicrosecond =Specify in.

    datetimeIf you don't know how to usedatetimeLet's see the section.

  • Answer # 3

    In the case of datetime.datetime (), you can set the values ​​for year, month, day, hour, minite, second, and microsecond respectively.

    date = "2020-11-22T02: 18: 34.967"
    utc_split = datetime (year = int (date [0: 4]), month = int (date [5: 7]), day = int (date [8:10]), hour = int (date [11:13]] ), minute = int (date [14:16]), second = int (date [17:19]), microsecond = int (date [20:]))
    print (utc_split)
    # datetime.datetime (2020, 11, 22, 2, 18, 34, 967)