http://blog.csdn.net/pipisorry/article/details/46619179
re模块匹配规则见:http://blog.csdn.net/pipisorry/article/details/25909899
python正则表达式进行日志分析
日志分析时,假设给定的字符串:
char str = “10.10.1.1 [2015/04/22 +0800] /ab/cd/?test0=123&test2=234 xxxx”; 要从中获取2015/04/22、/ab/cd/和234等值。
str = “10.10.1.1 [2015/04/22 +0800] /ab/cd/?test0=123&test2=234 xxxx”
print(re.findall(“\d{4}/\d{2}/\d{2}|/\w{2}/\w{2}|(?<=test2=)\d+”, str))
用urllib2、re、os 模块下载文件的脚本
!/usr/bin/env pythonimporturllib2
importre
importos
URL=’ http://image.baidu.com/channel/wallpaper’
read=urllib2.urlopen(URL).read()
pat =re.compile(r’src=\’#\’” //.+?.js”>’)
urls=re.findall(pat,read)
fori inurls:
url=i.replace(‘src=\’#\’” /code>,”).replace(‘”>’,”)
try:
iread=urllib2.urlopen(url).read()
name=os.path.basename(url)
with open(name,’wb’) as jsname:
jsname.write(iread)
except:
printurl,”url error”
将所有文档中不同问题分别汇总到同一个文件中
def shift():
'''
筛选所有人不同问题的答案到对应的文件中
:return:
'''
INPUT_DIR = r'C:\Users\pi\Desktop\txts'
ANSWER_DIR = r'C:\Users\pi\Desktop\answers'
if not path.exists(ANSWER_DIR):
makedirs(ANSWER_DIR)
questions = ['在计科四班度过大学四年是一种怎样的体验', '对班上的哪位同学第一印象最深刻,简述原因', '简述让你印象深刻的班上某位同学的一件小事', '想对你的室友说什么', '大学你最遗憾的事',
'最难忘或最感动或最想珍惜的一件事或人', '马上要毕业了,此刻的你内心是一种怎样的体验', '想对你暗恋or前女友or现任恋人说什么', '(写完后记得将文档重命名加上你的姓名)']
answer_filenames = [str(i) + '.txt' for i in range(len(questions) - 1)]
for doc_name in listdir(INPUT_DIR):
print(doc_name)
doc_full_name = path.join(INPUT_DIR, doc_name)
# print(doc_full_name)
open(doc_full_name)
with open(doc_full_name) as doc_file:
# file_str = ''.join(doc_file.read().split('\n'))
file_str = doc_file.read()
# print(file_str)
for cur_question, post_question, answer_filename in zip(questions, questions[1:], answer_filenames):
patten = cur_question + '[::](.+)' + post_question
try:
answer = re.findall(patten, file_str, re.S)[0]
# print(answer)
answer = re.split('\.|\d+', doc_name)[1] + ':' + answer.strip() + '\n'
print(answer)
with open(path.join(ANSWER_DIR, answer_filename), 'a') as answer_file:
answer_file.write(answer)
except:
print(RED, "Error in file:", DEFAULT, doc_full_name, RED, "question:", DEFAULT, cur_question)
# break
# break
from:
http://blog.csdn.net/pipisorry/article/details/46619179
ref:python模块 - re模块