I have a textfile and the input is:
-
abcdefg@hi
-
jklmnop@qr
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)