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.
文件比较,写一个比较两个文本文件的程序,如果不同,给出第一个不同的行号和列号
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.
相关文章推荐
- [置顶] 通过python利用哈希值实现比较两个文件的一致性
- 通过Python模块filecmp 对文件比较的实现方法
- [Python]re+mmap实现大文件的正则匹配
- Python3.5 实现xml 文件比较,比较pom.xml 中引用不一样的地方
- 在目录及其子目录下寻找匹配的文件的python实现
- Python 实现比较文件内容异同
- 用python实现文件比较
- 使用python实现正则匹配检索远端FTP目录下的文件
- python实现比较文件内容异同
- ASP.NET实现文件下载(多种方法,而且都是比较常用的)
- 文件通配符匹配检测C算法实现
- Python 文件操作实现代码
- python实现查找c cpp的文件[原创]
- python 实现生成目录树及显示文件大小
- 将C++代码全部写到头文件:)python脚本帮助自动生成相应的实现文件初始框架
- Python实现文件上传下载的SOAP Client
- Python实现ini文件操作
- 用python实现的可以拷贝或剪切一个文件列表中的所有文件
- Python open读写文件实现脚本
- 用标准模板库STL实现文件比较