码迷,mamicode.com
首页 > 编程语言 > 详细

Python re 截取文本中IP地址及用户名

时间:2019-08-22 13:04:30      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:lis   pid   dal   coding   eve   res   with open   nbsp   用户   

 

文本示例:

ts=2019-07-10T06:43:06523942Z pid=1875 tid=6320 version=e73c536 proto=http id=5a61a613e395f8832a372e4c1804ce10 http_url="/dev/ng.git/git-upload-pack" ip=10.10.5.9 xff_ip=10.10.5.9 user=wangdongdong repo=dev/ng repo_public=0 cmd=git-upload-pack fs_host=git-server-0b2c6f4a-a72d-11e8-9cba-000c294988e0 ac_ms=46.699 duration_ms=58.204 sr=1562740986465.729 ss=1562740986523.934 fs_sent=945 fs_recv=904 client_recv=1295 client_sent=1202 fsc_ms=0.274 log_level=INFO msg="http op done: (200)" http_status=200 imode=0
ts=2019-07-10T06:43:06603025Z pid=1875 tid=6323 version=e73c536 proto=http id=4930c2b5ce76d270a9d5b406433f3d55 http_url="/dev/ng.git/git-upload-pack" ip=10.10.5.9 xff_ip=10.10.5.9 user=wangdongdong repo=dev/ng repo_public=0 cmd=git-upload-pack fs_host=git-server-0b2c6f4a-a72d-11e8-9cba-000c294988e0 ac_ms=44.971 duration_ms=59.885 sr=1562740986543.132 ss=1562740986603.016 fs_sent=945 fs_recv=1184 client_recv=1420 client_sent=1470 fsc_ms=0.255 log_level=INFO msg="http op done: (200)" http_status=200 imode=0

  

 

import os
import re

file = open("E:\\desktop\\aaa.csv" , "w" , encoding=‘utf-8‘)
with open("E:\desktop\GitHub_babeld_Log\\babeld.log" , "r" , encoding=‘utf-8‘) as da:
    for line in da:
        username = re.findall(r"(user=(\w){1,20}(-)?(\w){1,20} )", line)
        ip_list = re.findall(r‘(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d)\.(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d)\.(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d)\.(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d)‘,line)
        if len(username) != 0:
            ip = ip_list[0]
            res = "".join(username[0]) + ‘,‘ + ".".join(ip)
            file.write(res+"\n")

          
file.close()

  

Python re 截取文本中IP地址及用户名

标签:lis   pid   dal   coding   eve   res   with open   nbsp   用户   

原文地址:https://www.cnblogs.com/xingfuxiaokong/p/11393466.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!