gpt4 book ai didi

python - 日期时间戳无法正确读取为字符串

转载 作者:行者123 更新时间:2023-12-01 06:48:18 24 4
gpt4 key购买 nike

        import pandas as pd
import json
import urllib.request
from time import time, sleep
from datetime import datetime


#
#
# Still need to add DateTime stamp to each entry and save DataFrame to csv file with self updating name
#
#

# This only works for API's that return a string formatted like a dictionary


# ticker_url is the link to your market
# y_seconds is how many seconds code waits before running again. Typically API requests are limited in this
# case it is limited to 1 time per second
#
# You may need to adjust your column names You can check column name by uncommenting and running this code below
#
# ticker_url = 'https://api.bittrex.com/v3/markets/btc-usdt/ticker'
# with urllib.request.urlopen(ticker_url) as response:
# html = response.read()

ticker_url = 'https://api.bittrex.com/v3/markets/btc-usdt/ticker'
interval_in_hours = .01
end_time = time() + interval_in_hours * 3600
y_seconds = 1
ticker_list = pd.DataFrame(columns = ['Symbol' , 'Last Trade', 'Bid' , 'Ask'])

while time() < end_time:

with urllib.request.urlopen(ticker_url) as response:
html = response.read()

ticker_data = json.loads(html)
ticker_list = ticker_list.append(ticker_data , ignore_index=True)
sleep(y_seconds)

ticker_list.to_csv('market_data'+str(datetime.timestamp)+'.csv')

我对编辑过程有一些注意事项,但我现在遇到的主要问题是格式化最后一行:


ticker_list.to_csv('market_data'+str(datetime.timestamp)+'.csv')

这段代码创建了这个文件


market_data<method 'timestamp' of 'datetime.datetime' objects>.csv

我想让它读取market_data20191130133030

2019 年 11 月 30 日下午 1:30 30 秒。我想要时间戳来读取文件创建时间。

谁能帮我解决这个问题吗?

最佳答案

datetime.timestamp()function 。因此,将您调用的行更改为:

ticker_list.to_csv('market_data'+str(datetime.timestamp())+'.csv')

但是,datetime.timestamp() 返回 POSIX 时间戳(自 1970 年 1 月 1 日以来的秒数)。如果您想要 YYYYMMDDHHMMSS 格式的时间戳,则需要使用 datetime.strftime(format)。您将使用的格式是%Y%m%d%H%M%S

关于python - 日期时间戳无法正确读取为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59127068/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com