如果它具有特定字符,则从阅读行跳过python

时间:2015-10-24 18:50:59

标签: python regex

这是foo.txt

中的输入文件数据
int i = void;

我想以下列格式输出数据

Wildthing83:A1106P
http://Wink3319:Camelot1@members/
f.signat@cnb.fr:arondep60

这是代码

f.signat@cnb.fr:arondep60
Wildthing83:A1106P
fr:arondep60

这是我的输出。

   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

正如你所知,它输出fr:arondep60并且我不想要它。有没有办法消除python阅读有@符号的行?即使看着它也会消除python

1 个答案:

答案 0 :(得分:1)

非常丑陋的解决方案,但它应该有用。

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