将代码输出格式化为 table 格式
formatting output of code into table format
我这里有以下代码管理网站的网络抓取信息,但我希望每 10 秒 运行 这段代码刷新一次 运行ning 这段代码以及格式化将此代码输出到一个漂亮的 table 中,其中包含前 5 个值的平均值。我应该怎么做?
import json
import requests
url = 'https://otc-api-hk.eiijo.cn/v1/data/trade-market?coinId=2¤cy=3&tradeType=sell&blockType=general'
data = requests.get(url).json()
# uncomment this to print all data:
# print(json.dumps(data, indent=4))
print('USDT SGD')
print('----')
for d in data['data']:
print('{:<30}{}'.format(d['userName'], d['price']))
url = 'https://otc-api.hbg.com/v1/data/trade-market?coinId=1¤cy=3&tradeType=sell&blockType=general'
data = requests.get(url).json()
print('BTC SGD')
print('----')
for d in data['data']:
print('{:<30}{}'.format(d['userName'], d['price']))
转换成pandas数据框,用nlargest计算平均值,打印数据和平均值
df = pd.Dataframe(data['data'])
df = df[['userName','price']]
top_5_avg = df.nlargest(5, "price")['price].mean()
print(df)
print(f'The average of top 5 is {top_5_avg}')
我这里有以下代码管理网站的网络抓取信息,但我希望每 10 秒 运行 这段代码刷新一次 运行ning 这段代码以及格式化将此代码输出到一个漂亮的 table 中,其中包含前 5 个值的平均值。我应该怎么做?
import json
import requests
url = 'https://otc-api-hk.eiijo.cn/v1/data/trade-market?coinId=2¤cy=3&tradeType=sell&blockType=general'
data = requests.get(url).json()
# uncomment this to print all data:
# print(json.dumps(data, indent=4))
print('USDT SGD')
print('----')
for d in data['data']:
print('{:<30}{}'.format(d['userName'], d['price']))
url = 'https://otc-api.hbg.com/v1/data/trade-market?coinId=1¤cy=3&tradeType=sell&blockType=general'
data = requests.get(url).json()
print('BTC SGD')
print('----')
for d in data['data']:
print('{:<30}{}'.format(d['userName'], d['price']))
转换成pandas数据框,用nlargest计算平均值,打印数据和平均值
df = pd.Dataframe(data['data'])
df = df[['userName','price']]
top_5_avg = df.nlargest(5, "price")['price].mean()
print(df)
print(f'The average of top 5 is {top_5_avg}')