我有一个带有一些网址的文本文件和“ - ”sysmbols。我想找出是否有与用户提供的域名不匹配的网址?如果是这样我必须打印一条消息,否则如果它只是给定的域名而“ - ”符号是另一条消息?我该怎么做?感谢
#!/usr/bin/python
import re
import string
print 'Enter Your Domain Name'
domain = input()
foo = open('new.txt','r')
seen = set()
lines = foo.readlines()
for line in lines:
match = re.search(domain,line)
for line in lines:
match = re.search(domain,line)
if match: seen.add("Message1")
else: seen.add('Message2')
foo.close()
示例文本文件:
http://www.mysite.com
-
http://www.mysite.com
http://www.yoursite.com
-
http://www.mysite.com
http://www.yoursite.com
答案 0 :(得分:0)
这是我的代码版本! ;) 尝试使用raw_input()而不是input()
如果该行与域匹配则可以。否则将其与“ - ”匹配。
#!/usr/bin/python
import re
domain = raw_input('Enter Your Domain Name: ')
foo = open('new.txt','r')
lines = foo.readlines()
foo.close()
ok_count = 0
not_ok_count = 0
for line in lines:
match = re.search(domain,line)
if match:
ok_count += 1
else:
match = re.search("-",line)
if match:
ok_count += 1
else:
not_ok_count += 1
if not_ok_count == 0:
print "File contains valid domains"
else:
print "File contains invalid domains"
希望这有帮助!
的Vivek