Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
179 views
in Technique[技术] by (71.8m points)

python - Posting sorted json data into a Discord table?

#remove stocks with 0 mentions
stock_with_mentions = dict((k, v) for k, v in stocks.items() if v >= 50)
#sort by value
sorted_stocks = {k: v for k, v in sorted(stock_with_mentions.items(), key=lambda item: item[1], reverse=True)}
print("STOCK MENTIONS")
print(sorted_stocks)

msg = Webhook(discordurl,msg="Stock Mentions WallStreetBets: 

"+str(sorted_stocks)+"")
msg.post()

This code so far outputs it to Discord like this:

https://i.imgur.com/depnH6g.png

But I want it to be a table with 2 columns like this on Discord using embeds or whatever other way there is.

enter image description here

Any help would be much appreciated. Thanks!

question from:https://stackoverflow.com/questions/65838005/posting-sorted-json-data-into-a-discord-table

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use tabulate to convert dictionaries into tables.

from tabulate import tabulate


msg_string = f'>>> `?`?`?prolog
{tabulate(list(sorted_stocks.items()), 
             headers=("Stock Ticker", "Mentions"), 
             tablefmt="fancy_grid")}
`?`?`?' # sorted_stocks is a dict
#send message

looks like this with the prolog discord markdown, you can change tablefmt argument or the discord markdown language to get different looks.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...