JohnT
JohnT

Reputation: 55

Skip python from reading line if it has a certain character

This is the input file data in foo.txt

Wildthing83:A1106P
http://Wink3319:Camelot1@members/
[email protected]:arondep60

And I wanna output the data in the following format

[email protected]:arondep60
Wildthing83:A1106P
fr:arondep60

Here is the code

   import re
f = open('foo.txt','r')


matches = re.findall(r'(\w+:\w+)@',f.read())
for match in matches:
    print match
f.seek(0)    
matches = re.findall(r'([\w.]+@[\w.]+:\w+)',f.read())
for match in matches:
    print match

f.seek(0)    
matches = re.findall(r'(\w+:\w+)\n',f.read())
for match in matches:
    print match

Here is my output.

Wink3319:Camelot1
[email protected]:arondep60
Wildthing83:A1106P
fr:arondep60

As you can tell, it's outputting fr:arondep60 and I don't want it to. Is there a way to eliminate python from reading a line that has any @ symbol? This would eliminate python even looking at it

Upvotes: 0

Views: 687

Answers (1)

Daffyd
Daffyd

Reputation: 104

Pretty ugly solution, but it should work.

line = f.readline()
if not "@" in line:
    matches = re.findall(r'(\w+:\w+)@',line)
    for match in matches:
            print match

line = f.readline()
if not "@" in line:
    matches = re.findall(r'([\w.]+@[\w.]+:\w+)',f.read())
    for match in matches:
            print match

line = f.readline()
if not "@" in line:  
    matches = re.findall(r'(\w+:\w+)\n',f.read())
    for match in matches:
        print match

Upvotes: 1

Related Questions