【py】 txt处理
2015-08-13 11:32
218 查看
处理一个txt,注意如下:
1、match是从头匹配,不匹配停止;search不停止
2、group()对匹配结果分组,group(0)是原匹配结果
3、lines循环
有用的在线工具:http://tool.chinaz.com/regex/
1、match是从头匹配,不匹配停止;search不停止
2、group()对匹配结果分组,group(0)是原匹配结果
3、lines循环
有用的在线工具:http://tool.chinaz.com/regex/
#!/usr/bin/python #coding=utf-8 import os import re print(os.name) # pattern=re.compile(r'\w+') outPut_file= open("E:\\Aglorithm_Data\\bgptable_out.txt", mode='w') input_file= open("E:\\Aglorithm_Data\\bgptable_in.txt", mode='r') for str in input_file.readlines(): # str=input_file.readline() print(str) # m1=re.search(r'(\d+\.\d+\.\d+\.\d+\/d+\s\d+\.\d+\.\d+\.\d+)',str) m1=re.match(r'\*\>\s+(\d+\.\d+\.\d+\.\d+\/\d+\s+\d+\.\d+\.\d+\.\d)',str) print(m1) if m1: print(m1.group(0)) print(m1.group(1)) # print(m1.group(2)) outPut_file.write(m1.group(1)+"\n") input_file.close outPut_file.close()
相关文章推荐
- 程序集引用异常 处理 app.config内控制runtime运行时应用的程序集版本指向 assemblyBinding结点 bindingRedirect
- 利用反射来改变String对象
- nginx安装配置
- CDN和镜像站点比较
- java并发编程--AbstractQueuedSynchronizer公平锁和非公平锁分析(三)
- java并发编程--AbstractQueuedSynchronizer条件锁分析(四)
- 相对布局LinearLayout权重weight的用法
- c#日期和随机数--》的士发票
- POJ 2250(LCS最长公共子序列)
- explain mysql性能优化
- C–K&R C 与 ANSI C的区别
- hdu-3371 Connect the Cities
- shell 批量创建用户 并指出错误?
- Fibonacci Tree 4786
- 调试存储过程报权限不足:ORA-0131 Insufficient privileges.
- java并发编程--AbstractQueuedSynchronizer的lock()和lockInterruptibly()方法分析(五)
- discuz 门户diy实现翻页功能的修改记录
- gradle 常用命令参数
- 改变tabbar的高度做法
- 浅谈SQL语句优化经验