1. 写正则表达式时,为什么要加上re.S
Make the ‘.‘ special character match any character at all, including a newline; without this flag, ‘.‘ will match anything except a newline.
2. 正则匹配中search,match,findall的差别
match()函数只检测RE是不是在string的开始位置匹配,search()会扫描整个string查找匹配,也就是说match()只有在0位置匹配成功的话才有返回,如果不是开始位置匹配成功的话,match()就返回none。search()会扫描整个字符串并返回第一个成功的匹配。
下面这段话时官方文档中对findall的解释:
Return all non-overlapping matches of pattern in string, as a list of strings. The string is scanned left-to-right, and matches are returned in the order found. If one or more groups are present in the pattern, return a list of groups; this will be a list of tuples if the pattern has more than one group. Empty matches are included in the result unless they touch the beginning of another match.
3. 对文件夹的的常用操作,判断是否以创建,不存在则创建,并把工作目录设成当前文件夹下
if not os.path.exists(KEYWORD): os.mkdir(KEYWORD) os.chdir(KEYWORD)
4. 其它常用操作
切片操作:i=i.replace(r"\\\\/","/") i=i[:-2]
这两步就可以完成所有字符串的替换。
完成动态路径的指定:file_path=‘{0}.{1}‘.format(i,‘jpg‘)。
判断某个类是否属于某个数据类型:
if isinstance(text,str) is not Ture
return None
原文地址:https://www.cnblogs.com/nanjingli/p/8952978.html