使用Python编写提取日志中的中文的脚本的方法


Posted in Python onApril 30, 2015

由于工作需要在一大堆日志里面提取相应的一些固定字符,如果单纯靠手工取提取,数据量大,劳心劳力,于是自然而然想到了用Python做一个对应的提取工具,代替手工提取的繁杂,涉及中文字符,正则表达式不好匹配,但不是不可以实现,这个以后优化时再说。

需求描述:

一个父目录中存在多个子文件夹,子文件夹下有多个txt形式化的Log日志,要求从所有地方Log日志中找出CardType=9, CardNo=0时的CardID的值,并将其统计存储到一个文本文件中,要求CardID不能够重复。

需求解析:

首先获取所有的Log日志的全路径,根据路径分别加载到将各个Log日志加载到内存中进行提取分析,并将结果存储到给定的文本文件中。

解决方案:

为了尽可能的简洁通用,这里使用配置文件作为输入变量的依据。不多说,上代码:

配置文件如下:

使用Python编写提取日志中的中文的脚本的方法

103文件夹下有两个文件:log1.txt和log2.txt, 内容类似如下:

使用Python编写提取日志中的中文的脚本的方法使用Python编写提取日志中的中文的脚本的方法

Python代码实现如下:

# -*- coding: utf-8 -*-
#!/usr/bin/python
# filename: picktools.py
# codedtime:2015-3-25

import os
import configparser

# 遍历一个目录,输出所有文件名
def itemsbrowse(path):
  for home, dirs, files in os.walk(path):
    for filename in files:
      yield os.path.join(home, filename)

# 给的文件中查找对应的字符串所在行      
def findchars(filename, chars):
  file = open(filename, 'r')
  for eachline in file:
    if eachline.find(chars) >= 0:
      yield eachline
  file.close()

# 添加到指定的文件
def addtofile(filename, mygenerator):
  file = open(filename, 'a')   # 追加方式打开
  for line in mygenerator:
    file.write(line)
  file.close()

# 过滤重复的字符行
def filter(filename):
  mylist = []
  file = open(filename, 'r')
  for eachline in file:
    mylist.append(eachline.strip())
  file.close()
  
  file2 = open(os.path.splitext(filename)[0] + '_filter.txt', 'w')
  for line in list(set(mylist)):
    print(line, file = file2)
    #file2.write(line) 
  file2.close()
  

def excute():
  iniconf = configparser.ConfigParser()
  iniconf.read('config.ini')
  ifile = iniconf.get('setting', 'ifilepath')
  ofile = iniconf.get('setting', 'ofilepath')
  chars = iniconf.get('setting', 'searchstr')
  
  for fullname in itemsbrowse(ifile):
    mygenerator = findchars(fullname, chars)
    addtofile(ofile, mygenerator)
    
  filter(ofile)
      
      
if __name__ == '__main__':
  excute()

输出结果:输出两个文件result.txt 和result_filter.txt

使用Python编写提取日志中的中文的脚本的方法使用Python编写提取日志中的中文的脚本的方法

心得体会:

1、利用Python去处理一些日常的小任务,可以很方便的完成,相比较C/C++来说,这方面生产力高了不少。

2、本文设计对中文字符的处理,所以使用正则表达式不太怎么方便,但不少不可以,后续版本中会添加对正则的支持!

3、由于初学中,所以代码写的不够精炼简洁,后续进行再优化!

Python 相关文章推荐
Python中logging模块的用法实例
Sep 29 Python
Python利用IPython提高开发效率
Aug 10 Python
Python实现小数转化为百分数的格式化输出方法示例
Sep 20 Python
python Flask实现restful api service
Dec 04 Python
Anaconda入门使用总结
Apr 05 Python
Django配置celery(非djcelery)执行异步任务和定时任务
Jul 16 Python
详解python里的命名规范
Jul 16 Python
Python基础知识点 初识Python.md
May 14 Python
Tensorflow 实现分批量读取数据
Jan 04 Python
python实现俄罗斯方块小游戏
Apr 24 Python
ansible-playbook实现自动部署KVM及安装python3的详细教程
May 11 Python
python中round函数如何使用
Jun 19 Python
简单的连接MySQL与Python的Bottle框架的方法
Apr 30 #Python
Python的Bottle框架中实现最基本的get和post的方法的教程
Apr 30 #Python
Python中使用Beautiful Soup库的超详细教程
Apr 30 #Python
Python中正则表达式的详细教程
Apr 30 #Python
详解在Python程序中使用Cookie的教程
Apr 30 #Python
处理Python中的URLError异常的方法
Apr 30 #Python
介绍Python的Urllib库的一些高级用法
Apr 30 #Python
You might like
php中$this->含义分析
2009/11/29 PHP
php从给定url获取文件扩展名的方法
2015/03/14 PHP
使用PHP similar text计算两个字符串相似度
2015/11/06 PHP
PHP数据库处理封装类实例
2016/12/24 PHP
页面版文本框智能提示JS代码
2009/11/20 Javascript
SyntaxHighlighter语法高亮插件使用说明
2011/08/14 Javascript
JS判断当前日期是否大于某个日期的实现代码
2012/09/02 Javascript
顶部缓冲下拉菜单导航特效的JS代码
2013/08/27 Javascript
JS实现从顶部下拉显示的带动画QQ客服特效代码
2015/10/24 Javascript
Javascript中prototype的使用详解
2016/06/18 Javascript
js前端解决跨域问题的8种方案(最新最全)
2016/11/18 Javascript
浅谈使用splice函数对数组中的元素进行删除时的注意事项
2016/12/04 Javascript
教你用十行node.js代码读取docx的文本
2017/03/08 Javascript
vue swipe自定义组件实现轮播效果
2019/07/03 Javascript
Python脚本判断 Linux 是否运行在虚拟机上
2015/04/25 Python
python数据预处理之将类别数据转换为数值的方法
2017/07/05 Python
Python中使用支持向量机(SVM)算法
2017/12/26 Python
Python中生成器和迭代器的区别详解
2018/02/10 Python
Python 实现文件打包、上传与校验的方法
2019/02/13 Python
Python 使用folium绘制leaflet地图的实现方法
2019/07/05 Python
Python寻找路径和查找文件路径的示例
2019/07/10 Python
Python如何输出警告信息
2020/07/30 Python
python中turtle库的简单使用教程
2020/11/11 Python
python字典与json转换的方法总结
2020/12/28 Python
利用python查看数组中的所有元素是否相同
2021/01/08 Python
解决pycharm 格式报错tabs和space不一致问题
2021/02/26 Python
网站开发实习生的自我评价
2013/12/11 职场文书
大型晚会策划方案
2014/02/06 职场文书
董事长助理岗位职责
2014/02/18 职场文书
关于长城的导游词
2015/01/30 职场文书
解除劳动合同通知书范本
2015/04/16 职场文书
2015年政风行风工作总结
2015/04/21 职场文书
小学教研工作总结2015
2015/05/13 职场文书
java Nio使用NioSocket客户端与服务端交互实现方式
2021/06/15 Java/Android
vue postcss-px2rem 自适应布局
2022/05/15 Vue.js
python如何查找列表中元素的位置
2022/05/30 Python