1.文件比較. 寫一個比較兩個文本文件的程序. 如果不同, 給出第一個不同處的行號和列號.
#!/usr/bin/env python
#coding:utf-8
import os.path
def filecompare():
i = 0
j = 0
while True:
file1 = raw_input("please input one filename:")
file2 = raw_input("please input one filename:")
if (os.path.exists(file1) == False):
print file1,"is not exists!"
continue
if (os.path.exists(file2) == False):
print file2,"is not exists!"
continue
break
fp1 = open(file1,'r')
fp2 = open(file2,'r')
filestr1 = fp1.readlines()
filestr2 = fp2.readlines()
for i in range(0,len(filestr1)):
if filestr1[i] != filestr2[i]:
for j in range(0,len(filestr1)):
if filestr1[i][j] != filestr2[i][j]:
break
break
print "For the first time is different in the %d row in the %d column" %(i+1,j+1)
filecompare()
2.匹配簡單的以“www.”開頭,以“.com”作結尾的 Web 域名,例如:www.yahoo.com. 附加題:使你寫的正則表達式還支持其他頂級域名:.edu, .net 等,比如:www.ucsc.edu.
pattern = r'\bwww\..*\.(com|net|edu)'