您的位置:首页 > 编程语言 > Python开发

python实现文件比较,匹配域名

2017-04-06 09:49 267 查看
题目1:

文件比较,写一个比较两个文本文件的程序,如果不同,给出第一个不同的行号和列号

  1 #!/usr/bin/env python

  2 fp1 = open('file1')

  3 fp2 = open('file2')

  4 count1=0

  5 count2=0

  6 count=0

  7 t1=fp1.readline()

  8 t2=fp2.readline()

  9 while t1!='' and t2 != '':

 10         if t1 == t2:

 11                 count1 = count1+1

 12         else:

 13                 count1 = count1+1

 14                 print "row:",count1

 15                 for i in range(min(len(t1),len(t2))):

 16                         if t1[i] == t2[i]:

 17                                 count2=count2+1

 18                                 if i==min(len(t1),len(t2))-1 and t1[i]!=t2[i    ]:

 19                                         print "col",i+2

 20                                         count=count+1

 21                                         break

 22                         elif t1[i] != t2[i]:

 23                                 count2=count2+1

 24                                 print "col:",count2

 25                                 count=count+1

 26                                 break

 27

 28         t1=fp1.readline()

 29         t2=fp2.readline()

 30         if count == 1:

 31                 break

file1内容

 1 u

  2 2

  3 4

file2内容

  1 wu

  2 1

  3 2

  4 hu

  5 4

运行:

[kiosk@foundation90 jben]$ python fileCom.py

row: 1

col: 1

题目2:

匹配简单的以“www.”开头,以“.com”作结尾ie的Web域名,例如:www.yahoo.com.附加题:使你写的正则表达式还支持其他顶级域名:.edu,.net等,比如:www.ucsc.edu.

代码:

  1 #!/usr/bin/env python

  2 d=["www.yahoo.com.","www.xian.edu.","news.xian.net.","news.badu.com.","www.qq.com."]

  3 myin1,myin2 = raw_input().split(" ")

  4 t=[]

  5 #print myin1," ... ",myin2

  6 for i in range(len(d)):

  7         if d[i].startswith(myin1) and d[i].endswith(myin2):

  8                 t.append(i)

  9                 print d[i]

 10 #       elif d[i].startswith("www.") and d[i].endswith(".edu"):

 11 #               print d[i]

 12 #       elif d[i].startswith("news.") and d[i].endswith(".net"):

 13 #               print d[i]

 14 if len(t)==0:

 15         print "domain you found is not exist!"

运行:

[root@foundation90 jben]# python matchDomain.py

www. .com.

www.yahoo.com.

www.qq.com.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: