Tiêu đề nói lên tất cả. Tôi đang viết một tập lệnh để thực hiện các yêu cầu nhận theo lịch trình cho API. Tôi muốn in khi cuộc gọi API tiếp theo sẽ được thực hiện, sẽ là 15 phút kể từ cuộc gọi trước đó.
Tôi rất thân, nhưng đã gặp phải lỗi sau: TypeError: a float is required
Đây là mã của tôi:
import time, datetime
from datetime import datetime, timedelta
while True:
## create a timestamp for the present moment:
currentTime = datetime.datetime.fromtimestamp[time.time[]].strftime["%Y-%m-%d %H:%M:%S"]
print "GET request @ " + str[currentTime]
## create a timestamp for 15 minutes into the future:
nextTime = datetime.datetime.now[] + datetime.timedelta[minutes = 15]
print "Next request @ " + str[datetime.datetime.fromtimestamp[nextTime].strftime["%Y-%m-%d %H:%M:%S"]
print "############################ DONE #############################"
time.sleep[900] ## call the api every 15 minutes
Tôi có thể khiến mọi thứ hoạt động [loại] khi thay đổi dòng sau:
print "Next request @ " + str[nextTime]
Tuy nhiên, điều này in một dấu thời gian với sáu vị trí thập phân cho mili giây. Tôi muốn giữ mọi thứ ở định dạng %Y-%m-%d %H:%M:%S
.
Seanny123
8.16312 Huy hiệu vàng67 Huy hiệu bạc119 Huy hiệu đồng12 gold badges67 silver badges119 bronze badges
Đã hỏi ngày 3 tháng 4 năm 2017 lúc 19:17Apr 3, 2017 at 19:17
Philiporlandophiliporlandophiliporlando
8814 Huy hiệu vàng19 Huy hiệu bạc29 Huy hiệu đồng4 gold badges19 silver badges29 bronze badges
1
Bạn không cần phải sử dụng datetime.fromtimestamp
vì
print "Next request @ " + str[nextTime]
0 đã là một đối tượng DateTime [và không phải là một chiếc phao]. Vì vậy, chỉ cần sử dụng:nextTime = datetime.datetime.now[] + datetime.timedelta[minutes = 15]
print "Next request @ " + nextTime.strftime["%Y-%m-%d %H:%M:%S"]
Đã trả lời ngày 3 tháng 4 năm 2017 lúc 19:24Apr 3, 2017 at 19:24
0
Bạn có thể đạt được nó chỉ bằng cách sử dụng dấu thời gian thay thế: thay vào đó:
import time
from datetime import datetime
while True:
# create a timestamp for the present moment:
currentTime_timestamp = time.time[]
currentTime = datetime.fromtimestamp[
currentTime_timestamp
].strftime["%Y-%m-%d %H:%M:%S"]
print "GET request @ " + str[currentTime]
# create a timestamp for 15 minutes into the future:
nextTime = currentTime_timestamp + 900 # 15min = 900 seconds
print "Next request @ " + str[datetime.fromtimestamp[
nextTime
].strftime["%Y-%m-%d %H:%M:%S"]]
print "############################ DONE #############################"
time.sleep[900] # call the api every 15 minutes
Đầu ra tôi nhận được là:
GET request @ 2017-04-03 16:31:34
Next request @ 2017-04-03 16:46:34
############################ DONE #############################
Đã trả lời ngày 3 tháng 4 năm 2017 lúc 19:33Apr 3, 2017 at 19:33
Joab Mendesjoab MendesJoab Mendes
1.0101 Huy hiệu vàng19 Huy hiệu bạc32 Huy hiệu đồng1 gold badge19 silver badges32 bronze badges
Đóng. Không cần phải sử dụng
print "Next request @ " + str[nextTime]
1 ở đây. Bạn có thể giảm vài dòng cuối cùng thành:import datetime as dt
nextTime = dt.datetime.now[] + dt.timedelta[minutes = 15]
print "Next request @ " + dt.datetime.strftime[nextTime, "%Y-%m-%d %H:%M:%S"]
Nói cách khác, chuyển đối tượng
print "Next request @ " + str[nextTime]
2 print "Next request @ " + str[nextTime]
0 là tham số đầu tiên cho print "Next request @ " + str[nextTime]
4.Đã trả lời ngày 3 tháng 4 năm 2017 lúc 19:24Apr 3, 2017 at 19:24
Bạn có thể đạt được nó chỉ bằng cách sử dụng dấu thời gian thay thế: thay vào đó:roganjosh
Đầu ra tôi nhận được là:4 gold badges29 silver badges44 bronze badges
2
Đã trả lời ngày 3 tháng 4 năm 2017 lúc 19:33
Joab Mendesjoab Mendes
import time, datetime
from datetime import datetime, timedelta
while True:
## create a timestamp for the present moment:
currentTime = datetime.datetime.fromtimestamp[time.time[]].strftime["%Y-%m-%d %H:%M:%S"]
print "GET request @ " + str[currentTime]
## create a timestamp for 15 minutes into the future:
nextTime = datetime.datetime.now[] + datetime.timedelta[minutes = 15]
print "Next request @ " + str[nextTime.strftime["%Y-%m-%d %H:%M:%S"]]
print "############################ DONE #############################"
time.sleep[900] ## call the api every 15 minutes
Đã trả lời ngày 3 tháng 4 năm 2017 lúc 19:24Apr 3, 2017 at 19:24
Philiporlandophiliporlandophiliporlando
8814 Huy hiệu vàng19 Huy hiệu bạc29 Huy hiệu đồng4 gold badges19 silver badges29 bronze badges