python:提取文本中的某几列解决方案

python:提取文本中的某几列
本帖最后由 chosen86 于 2014-08-15 14:18:58 编辑

[14/Jul/2014:16:39:22 CST] [4019943168] 10.6.99.163   test1 "CONNECT" STARTED 0 0 0 (10.6.99.163:63548 -> 10.203.19.28:8080)
[14/Jul/2014:16:39:24 CST] [4019808000] 10.6.99.163   test1 "CONNECT" ISERROR 0 0 - (10.6.99.163:63551  -> proxy.sgm.shanghaigm.com:8080)
要处理的文本中每一行都是这种形式,但是对我有用的只有红色标记的这四列,想问一下怎么把这四列提取出来写入到一个新文件中,谢谢!
------解决方案--------------------
import re
with open(r'C:\Users\admin-ZH\Desktop\111.txt', 'r') as fr:
    fw = open(r'C:\Users\admin-ZH\Desktop\222.txt', 'w+')
    for text in fr:
        times = re.findall(r'(?<=\[).*?(?= CST\])', text)[0]
        ips = re.findall(r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}', text)[0]
        test = re.findall(r'(?<={}).*?(?=")'.format(ips), text)[0].strip()
        ip_port = re.findall(r'(?<=->).*?(?=\))'.format(ips), text)[0].strip()
        infos = ' '.join([times, ips, test, ip_port]) + '\n' 
        fw.write(infos)
    fw.close()

with open(r'C:\Users\admin-ZH\Desktop\222.txt', 'r') as fr:
    for text in fr:
        print text

同为学生,还是劝楼主不要做伸手党。