![]() |
Extract email addresses from string and store them in a list - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Extract email addresses from string and store them in a list (/thread-22727.html) |
Extract email addresses from string and store them in a list - oslosurfer - Nov-24-2019 How can I extract email addresses from a string and store them in a list? The following code produces the desired output when printed in the for loop, but I am not sure how to store them in one accessible list. text = 'Emails: [email protected], [email protected], [email protected]' text = text.split() for word in text: if '@' in word: emails = word emails = emails.replace(',', '') emails = emails.split() print(emails)
RE: Extract email addresses from string and store them in a list - perfringo - Nov-24-2019 Little bit refactored with usage of list comprehension: 'give me chunk without ending comma for every chunk in list of chunks from text if chunk contains @'. >>> text = 'Emails: [email protected], [email protected], [email protected]' >>> [chunk.rstrip(',') for chunk in text.split() if '@' in chunk] ['[email protected]', '[email protected]', '[email protected]'] RE: Extract email addresses from string and store them in a list - oslosurfer - Nov-24-2019 Perfect! Thank you. |