2019-07-03:正则练习

#encoding=utf-8
"""
1 创建一个能从type()的结果中提取实际类型名称的正则表达式.返回int
"""
import re
s =""
print (re.search(r"'(\w+)'",s).group(1))

"""
2 请写出一个正则表达式表示标准的日历上的其他三个月(十月、十一月、十二月)
"""
import re
pattern = re.compile(r"1[0-2]")
print (pattern.match("10").group())
print (pattern.match("11").group())
print (pattern.match("12").group())


"""
3 统计生成的redata.txt文件中,星期中的每一天出现的次数
"""
"""
Fri Apr 12 02:04:19 2030::[email protected]::1902161059-7-10
Sun Sep  2 14:33:07 1973::[email protected]::115799587-6-8
Wed Aug 21 05:11:12 2030::[email protected]::1913490672-7-10
Sun Jan  6 08:14:19 1980::[email protected]::315965659-6-7
Sat Mar 29 15:17:51 1975::[email protected]::165309471-5-11
Wed Jul 22 19:47:29 1970::[email protected]::17495249-6-11
Thu May 12 08:08:08 2022::[email protected]::1652314088-6-10
Mon Jul 10 03:27:13 1978::[email protected]::268860433-5-11
"""

def findWeekDay(path):
    result={}
    days=[]
    with open(path,"r") as fp:
        for line in fp:
            pattern=re.compile(r"[A-Za-z]{3}")
            match=pattern.match(line).group()
            days.append(match)
    for day in days:
        result[day]=days.count(day)
    return result
print(findWeekDay("E:\\murphy\\test\\data.txt"))

"""
4 通过检查每个输出行中整型字段部分的第一个整型是否和
该行开头的时间戳相匹配来验证data.txt中的数据知否完好
"""
#这里的时间使用的是3题的数据
import re
import time

def compareTime(path):
    with open(path,"r") as fp1:
        for line in fp1:
            format_time=re.match(r"(.*?)::",line).group(1)
            times_tamp=float(re.search(r"::(\d+)",line).group(1))
            if format_time!=time.ctime(times_tamp):
                print(format_time)
                print(times_tamp)
                print("数据不完好")
            else:
                print("数据完好")
print(compareTime("E:\\murphy\\test\\data.txt"))

"""
5.提取每行中的完整时间字段
"""

times=[]
path="E:\\murphy\\test\\data.txt"
pattern=re.compile(r".*?::")
with open(path,"r") as fp2:
    for line in fp2:
        tampTime=pattern.match(line).group()
        times.append(tampTime)
print(times)

 

你可能感兴趣的:(Python)