版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/github_39655029/article/details/82694996
文件操作
with:自动关闭文件;with open('log', 'r') as f:
...open()模式匹配与正则表达式
re;re.compile()函数创建一个Regex对象(记得使用原始字符串);Regex对象的search()方法传入想要查找的字符串,返回一个Match对象;Match对象的group()方法,返回实际匹配文本的字符串;|,用于匹配多个表达式中的一个,匹配多个分组;?,实现可选匹配;>>> import re
>>> batRegex = re.compile(r'Bat(wo)?man')
>>> mo1 = batRegex.search('The Adventures of Batman.')
>>> print(mo1.group())
Batman
>>> mo2 = batRegex.search('The Adventures of Batwoman.')
>>> print(mo2.group())
Batwoman*,匹配零次或多次,即星号之前的分组,可以在文本中出现任意次;>>> import re
>>> batRegex = re.compile(r'Bat(wo)*man')
>>> mo1 = batRegex.search('The Adventures of Batwowoman')
>>> print(mo1.group())
Batwowoman+,匹配一次或多次,加号前面的分组必须"至少出现一次”;>>> import re
>>> batRegex = re.compile(r'Bat(wo)+man')
>>> mo1 = batRegex.search('The Adventures of Batwowoman')
>>> print(mo1.group())
Batwowoman
>>> mo2 = batRegex.search('The Adventures of Batman')
>>> print(mo2 == None)
True{ },匹配特定次数;>>> import re
>>> batRegex = re.compile(r'ha{3}')
>>> mo1 = batRegex.search('hahaha')
>>> print(mo1.group())
hahaha
>>> mo2 = batRegex.search('haha')
>>> print(mo2 == None)
Truefindall()方法返回结果: ['123-324-5832', '324-589-0983'];[('123', '453', '4324'), ('343', '654', '3245)];^xxx:表示字符串必须以xxx开始;xxx$:表示字符串必须以xxx结尾;open()函数,返回一个File对象;File对象的read()或write()方法;File对象的close()方法,关闭该文件;os.unlink(path)删除path处的文件;os.rmdir(path)将删除path处的文件夹,但文件夹必须为空;shutil.rmtree(path)删除path处的文件夹,包含的所有文件和文件夹都会被删除;调试