0

I'm adding each line from a file to a list and everything is working fine up until I check to see that the line contains "@" and ":" if "@" and ":" in line:. Logically it should work but it seems to be checking the only first item which is "@". If i put ":" first then it only check that rather than both.

email_list = []
with open(f"{file_path}/test.txt", "r") as file:
    for line in file:
        line = line.replace(' ', '')
        if not any(x in line for x in remove_emails):
            line = line.strip()
            if "@" and ":" in line: #This line is not working
                email_list.append(line)

Expected outcome:

if file contains:
user_1@domain:123
user_2@domain
user_3domain:123

Only user_1@domain:123 should be added to the list as it has both @ and :
squidg
  • 451
  • 6
  • 17
  • 2
    You might consider using [all](https://docs.python.org/3/library/functions.html#all) for less ambiguity. `all(ch in line for ch in ("@",":"))` – dawg May 20 '20 at 04:40

1 Answers1

3
if "@":
   print("Well this is always true")

if "@" and "X" in my_string:
   print("found X in my_string... '@' is always true ao maybe its in this string")

instead you need

if "@" in my_string and ":" in my_string:
   print("Both are here!")

if you have many characters you want to check you could use dawgs suggestion from the comments

to_check = ":@"
if all(ch in my_string for ch in to_check):
   print("I found all of {0} in my_string".format(to_check)
Joran Beasley
  • 110,522
  • 12
  • 160
  • 179