Getting name from the email address in python

Question:

I have a text file in which there are email addresses in the following format:

[email protected]

or

some_name@somehost

How can I split the string to get some & name? I tried the following code, but it appears I can only use one character (just the @ or just the .).Is there a way to do it in one statement? Thanks.

name = email.split('@', '.')
Asked By: Ekaterina1234

||

Answers:

Use the following:

email = "[email protected]"

name, _, _ = email.partition("@")  # returns before @, @, and after @
splitname = name.split(".")        # splits on .

print(splitname)

["some", "name"]

Answered By: Simon Kirsten

In case of email addresses with no predefined structure, you can use: https://github.com/intuit/email-decomposer.

Answered By: Elhanan Mishraky
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.