I have the following problem:
I have a string that contains multiple E-Mail adresses. These adresses are not static. I pull them from my database. So for example when the adresses are pulled and I print the string the output is:
mails = 'email1', 'email2', 'email3'
Now I want to make a list out of the string. So my code is:
list = [mails]
But when I print the list, I get the following result:
["'email1', 'email2', 'email3'"]
How can I remove the double quotes, so that the output looks like this?
['email1', 'email2', 'email3']
Thank you for your answers :)
CodePudding user response:
Using Regex and list comprehension:
import re
my_list = [re.sub(r"'([^'] )'", r"\1", x) for x in my_list]
Explanation: What is inside single quotes gets added to group which the replacement string \1 is referring to.
CodePudding user response:
Supposing you have, in your code:
mails = 'email1', 'email2', 'email3'
That is a tuple of strings, i.e.
('email1', 'email2', 'email3')
You can simply convert that into a list by casting:
mails = list(mails)
which will produce
['email1', 'email2', 'email3']
Of course, all depends on your input data. Normally you would retrieve the data from your database and add it instantly to a list or some data structure that supports the intended functionality on that data better than a list.
CodePudding user response:
Just Do this:
mails = 'email1', 'email2', 'email3'
mails= list(mails)
print(mails)
and now the result will be the same as you want:
['email1', 'email2', 'email3']
CodePudding user response:
import json
a_list = json.dumps(emails)
Is how i would do it If i wanted a string representation like what is described...
