How do I replace certain characters when messaging in discord.py

Question:

My issue is that I am trying to make a leader board command for my discord bot but when I sort the json file and then message that it looks like this:

{‘| Name’: 784779, ‘| Name_1’: 539427, ‘| Name_2’: 310407, ‘| Name_3’: 280250, ‘| Name_4’: 0}

its sorted and everything but it looks messy because of the brackets and quotes and also because it all on one line. I tried to fix this with replace() but it gave an error. Is there a different way to replace characters? my code

if "!leaderboard" in message.content:
    with open("income.json", "r+") as f:
        incomes = json.load(f)
    Sort = {k: v for k, v in sorted(incomes.items(), key=lambda item: item[1], reverse= True)}

    embedVar = discord.Embed(title= "Leader board",value=(""), inline=False, color=0x71368a)
    embedVar.add_field(name=('﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉'), value=(Sort))
    await message.channel.send(embed=embedVar)
Asked By: SF Mittens

||

Answers:

Sort is not a string but a dictionary, that’s why replace doesn’t work. It gets automatically converted to a string but if you wanna format it yourself, you will have to iterate through it and convert it manually.

my_string = ""
for k, v in Sort.items():
    my_string += str(k) + ": " + str(v) + "n"

Then pass my_string to embedVar.addfield instead of passing Sort. This is the basic idea, modify it to fit your needs.

Answered By: FIlip Müller
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.