Python re 截取文本中IP地址及用户名
Posted xingfuxiaokong
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python re 截取文本中IP地址及用户名相关的知识,希望对你有一定的参考价值。
文本示例:
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]\d2|[1-9]?\d)\.(25[0-5]|2[0-4]\d|[0-1]\d2|[1-9]?\d)\.(25[0-5]|2[0-4]\d|[0-1]\d2|[1-9]?\d)\.(25[0-5]|2[0-4]\d|[0-1]\d2|[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地址及用户名的主要内容,如果未能解决你的问题,请参考以下文章