Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to split a list

I have a textfile and the input is:

  • abcdefg@hi

  • jklmnop@qr

    MEDevel.com: Open-source for Healthcare and Education

    Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

    Visit Medevel

In the following I read every line into a list while stripping the \n:

with open(file) as f:
    return [email.strip() for email in f]

Output after printing the list: [abcdefg@hi, jklmnop@qr]

BUT I want to get only the part before the @ like abcdefg. I need to use split('@') and then get [0]. But in which line in the with-open-code above I need to split it?

Another question: Does the expression [email.strip() for email in f] has any specific name?

>Solution :

I would suggest using re.findall here:

with open(file) as f:
    data = f.read()
    emails = re.findall(r'(\S+)@\S+', data)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading