python中将正则过滤的内容输出写入到文件中
来源:互联网 发布:k均值聚类算法步骤 编辑:程序博客网 时间:2024/05/17 09:10
处理过滤Apache日志文件
access_test.log文件内容
27.19.74.143 - - [30/May/2015:17:38:21 +0800] "GET /static/image/smiley/default/sleepy.gif HTTP/1.1" 200 2375
8.35.201.164 - - [30/May/2015:17:38:21 +0800] "GET /static/image/common/pn.png HTTP/1.1" 200 592
过滤目标
60.166.12.170 31/May/2013:00:00:02 /forum.php 200 45780
处理后将内容写入到文件20160205.txt
#!/usr/bin/env python
# - coding:utf - 8 -*-
import re,sys
with open('access_test.log') as f:
for line in f:
parseip = re.search(r'(.*?) - - ', line)
parsetime = re.search(r' \[(.*?)\] ', line)
parseurl = re.search(r' "\w+ (.*?) HTTP/', line)
parsestatus = re.search(r' HTTP/(.*?)" (.*?) ', line)
parseTraffic = re.search(r'\d+ \d+', line)
if parseip and parsetime and parseurl and parsestatus and parseTraffic is None:
continue
output=sys.stdout
outputfile=open('20160205.txt','a')
sys.stdout=outputfile
print parseip.group(1).split('?')[0] + '\t' + parsetime.group(1).split('?')[0] + '\t' + parseurl.group(1).split('?')[0] + '\t' + parsestatus.group(2) + '\t' + parseTraffic.group(0).split(' ')[1]
outputfile.close()
sys.stdout=output
import sys
然后在打算把输出数据写入文件的代码之前加上以下代码
output=sys.stdout
outputfile=open(filename,'w')
sys.stdout=outputfile
上面的filename表示输出文件
程序结束或恢复成正常输出时加上以下代码
outputfile.close()
sys.stdout=output
恢复输出为开始保存的正常输出值
access_test.log文件内容
27.19.74.143 - - [30/May/2015:17:38:21 +0800] "GET /static/image/smiley/default/sleepy.gif HTTP/1.1" 200 2375
8.35.201.164 - - [30/May/2015:17:38:21 +0800] "GET /static/image/common/pn.png HTTP/1.1" 200 592
过滤目标
60.166.12.170 31/May/2013:00:00:02 /forum.php 200 45780
处理后将内容写入到文件20160205.txt
#!/usr/bin/env python
# - coding:utf - 8 -*-
import re,sys
with open('access_test.log') as f:
for line in f:
parseip = re.search(r'(.*?) - - ', line)
parsetime = re.search(r' \[(.*?)\] ', line)
parseurl = re.search(r' "\w+ (.*?) HTTP/', line)
parsestatus = re.search(r' HTTP/(.*?)" (.*?) ', line)
parseTraffic = re.search(r'\d+ \d+', line)
if parseip and parsetime and parseurl and parsestatus and parseTraffic is None:
continue
output=sys.stdout
outputfile=open('20160205.txt','a')
sys.stdout=outputfile
print parseip.group(1).split('?')[0] + '\t' + parsetime.group(1).split('?')[0] + '\t' + parseurl.group(1).split('?')[0] + '\t' + parsestatus.group(2) + '\t' + parseTraffic.group(0).split(' ')[1]
outputfile.close()
sys.stdout=output
import sys
然后在打算把输出数据写入文件的代码之前加上以下代码
output=sys.stdout
outputfile=open(filename,'w')
sys.stdout=outputfile
上面的filename表示输出文件
程序结束或恢复成正常输出时加上以下代码
outputfile.close()
sys.stdout=output
恢复输出为开始保存的正常输出值
0 0
- python中将正则过滤的内容输出写入到文件中
- python 中将一个函数的输出写入到文件中
- NET(C#)中将DATATABLE内容写入到CSV文件 与 将CSV文件内容读取到DATATABLE中
- 写入内容到文件中
- python 文件输出与重定向 ,输出的内容会在内存中暂存,不会立刻输出到文件中
- Delphi中将DBGRID中的内容输出到WORD中
- C中将格式化数据写入到文件中操作
- 将内容写入到文件中
- 【Python小工具】Python实现批量解析PDF文件提取内容并写入到Excel中
- Python 多线程搜索txt文件的内容,并写入搜到的内容(Lock)
- C++中将打印结果输出到文件中
- python给文件写入/追加内容时输出x00和乱码的问题
- playframework中将log输出到文件里的配置方法
- C++中将输出指定到文件的方法:
- 将一个文件的内容写入到另一个文件中(字节流)
- python分批定量读取文件内容,输出到不同文件中
- python---对python的help命令内容写入特定文件中
- 如何把控制台输入的内容写入到指定的文件中
- zookeeper3.4.5自动清理日志
- ios如何实现静音模式下声音仍然可以外放
- 集合工具类Collections用法(API简略翻译)
- python切分apache日志文件
- ulua
- python中将正则过滤的内容输出写入到文件中
- Archlinux 缺少<uuid/uuid.h> No such file or directory
- Python实现九九乘法表
- 八项提高机器学习模型的准确率的方法
- 搞清楚LzoCodec和LzopCodec
- 推荐!国外程序员整理的Java资源大全
- trie 403 Forbidden
- java.lang.OutOfMemoryError: Java heap space 的解决
- nginx访问日志没有内容