This question already has answers here:
Print list without brackets in a single row
(14 answers)
Closed 1 year ago.
I have a list of strings, name, ignore_stock. I want to display it in a telegram bot message. Presently, I am displaying it like this:
update.message.reply_text(f'<b>Following stocks are ignored: {ignore_stock}</b>', parse_mode='HTML')
This displays the list like this: ['a','b','c'] inside the telegram bot message.
I want to display the list like this: a, b, c inside the telegram bot message without [ ] and ''. How do I do that?
thanks in advance
Try replacing {ignore_stock} with {",".join(ignore_stock)}
Related
This question already has an answer here:
How to extract values from a Python request
(1 answer)
Closed 6 months ago.
How do I get a specific element from requests response.text
{"status":"success","data":{"id":30815925}}
im need to get ID from that output
You can import the json package and simply do
import json
id = json.loads(response.text)["data"]["id"]
This will convert response.text from a string to a python dictionary, and the id will be inside the variable id
I am building a Discord bot using the discord.py library. The library uses the on_message(ctx) event to let us modify a message if we want to. What I would like to do is to remove the formatting and replace the user mentions with the Discord names. The text of the message looks like this:
<#!34565734654367046435> <#!34565734645354367046435> are you here?
In the ctx variable I can get the mentions as User objects from ctx.mentions and I can get the NAME and the ID of each mentioned user.
I'd like to replace the <#!34565734654367046435> with the name of the user. The result to be something like:
Name1 Name2 are you here?
This is what I have so far but it does not seem to work.
remove_formatting = utils.remove_markdown(context.content)
print(remove_formatting) # prints the very first code styled line in this question
for member in context.mentions:
remove_formatting.replace(f'<#!{member.id}>', member.name)
If I print the member.id and the member.name without doing anything else, I get the expected values. How do I update the string in this for loop?
The replace function returns a new string, it doesn't edit the existing string in place. If you edit your loop to:
remove_formatting = utils.remove_markdown(context.content)
for member in context.mentions:
remove_formatting = remove_formatting.replace(f'<#!{member.id}>', member.name)
it should work.
This question already has answers here:
How to concatenate (join) items in a list to a single string
(11 answers)
Closed 2 years ago.
I'm relatively new to python,
I built a webscraper that gets the top posts of a website and stores them in a list in python like this:
T = ["post1","post2","post3,"post4"]
To send the push notification with pushover I installed the pushover module that works like this:
from pushover import Pushover
po = Pushover("My App Token")
po.user("My User Token")
msg = po.msg("Hello, World!")
po.send(msg)
I want to send the list as a message with format, like this:
Top Posts:
1. post 1
2. post 2
3. post 3
4. post 4
I tried this:
msg = po.msg("<b>Top Posts:</b>\n\n1. "+T[0]+"\n"+"2. "+T[1]+"\n"+"3. "+T[2]+"\n"+"4. "+T[3]")
The above solution works, however the number of posts will be variable so that's not a viable solution.
What can I do to send the message with the correct formatting knowing that the number of posts in the list will vary from time to time?
Using str.join and a comprehension using enumerate:
msg = po.msg("<b>Top Posts:</b>\n\n" + '\n'.join(f'{n}. s {n}' for n, s in enumerate(T, 1)))
This question already has answers here:
Print to the same line and not a new line? [duplicate]
(19 answers)
Closed 3 years ago.
I am using an API where I can fetch data with. From the data output I am trying to get specific portions so I can place it nicely in a formatted text.
I want to fetch the data every 5 seconds, so I get fresh info. I do not want that the data is prompted below the output from the first run, but rather replace the current value(s) for the updated value(s).
As I'm pretty bad with python, I hope someone can give me some advice.
import requests
import threading
def dostuff()
threading.Timer(5.0, dostuff).start()
r = requests.get('https://api')
data = r.json()
print("Amount:", data['amount'])
print("Games played:", data['matches'])
dostuff()
This works fine. It just keeps posting the output under each other.
I wish to have everything static, except the data['amount'], and data['matches'], which should keep updating without actually posting it on newlines. I have tried resolving this by clearning the screen, but that is not the desired solution.
Just add end='\r' to your print statement:
import requests
import threading
import random
def dostuff():
threading.Timer(1.0, dostuff).start()
# replaced as actual api not posted
data = {
'amount': round(random.random(), 2),
"matches": round(random.random(), 2)
}
print("Amount: {} Games played: {}".format(
data['amount'],
data['matches']
), end='\r')
dostuff()
This question already has answers here:
Is there a simple way to delete a list element by value?
(25 answers)
Closed 6 years ago.
I have the following structure on a JSON file:
{
"channels": [
"180873781382873088",
"181268808055521280",
"183484852287307777",
"174886257636147201",
"174521530573651968"
]
}
I want to know how I can loop through the file searching for a specific string, and delete it if it matches.
Thank you.
EDIT: A Google search pointed me to using a for loop and using the del command to remove the key, so here's what I tried:
channel = "180873781382873088"
for item in data['channels']:
del channel
But it only deletes the variable channel, not the key that matches it's value.
Try
data['channels'].remove(channel)
instead of the for loop.
This will automatically search the array and remove any key matching your variable. If you need help saving the results to a file I would open another question.