Python练习题

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)'
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值