如何使用regex在.csv文件中搜索ip范围内的所有行



我正在尝试使用regex搜索.csv文件,以返回ip地址在200.10.5.1-254范围内的所有行,并将它们附加到列表中。我对python有点陌生,所以如果我完全错了,我不会感到震惊。这是我迄今为止所拥有的。

#!/usr/bin/python3
import re, csv
def computers():
employees =[]
rows = []
with open("empdata.csv","r") as myfile:
data=csv.reader(myfile)
fields=next(myfile)
print(fields)
ipregex = '^200.10.5.([1-9]|[1-9]d|1dd|2[0-4]d|25[0-4]$'
for lines in myfile:
if re.findall(ipregex,myfile):
employees.append(lines)
print(employees)
computers()

这会返回以下错误:

first_name,last_name,email,ip_address,department,username,Password
Traceback (most recent call last):
File "/home/student/.config/JetBrains/PyCharmCE2020.1/scratches/Lab 8.py", line 16, in <module>
computers()
File "/home/student/.config/JetBrains/PyCharmCE2020.1/scratches/Lab 8.py", line 13, in computers
match=re.findall(ipregex,myfile)
File "/usr/local/lib/python3.7/re.py", line 223, in findall
return _compile(pattern, flags).findall(string)
File "/usr/local/lib/python3.7/re.py", line 286, in _compile
p = sre_compile.compile(pattern, flags)
File "/usr/local/lib/python3.7/sre_compile.py", line 764, in compile
p = sre_parse.parse(p, flags)
File "/usr/local/lib/python3.7/sre_parse.py", line 930, in parse
p = _parse_sub(source, pattern, flags & SRE_FLAG_VERBOSE, 0)
File "/usr/local/lib/python3.7/sre_parse.py", line 426, in _parse_sub
not nested and not items))
File "/usr/local/lib/python3.7/sre_parse.py", line 819, in _parse
source.tell() - start)
re.error: missing ), unterminated subpattern at position 13

以下是csv文件empdata.csv 的示例

first_name,last_name,email,ip_address,department,username,Password
Aaron,Stevens,astevens1c@dailymotion.com,144.246.48.229,Product Management,astevens1c,IW8JT2R8
Amy,King,akingm@barnesandnoble.com,37.103.166.158,Human Resources,akingm,yH2gyDQZfjp
Andrew,Bowman,abowmane@bbb.org,194.147.141.95,Marketing,abowmane,dZfErQSdKFi
Beverly,Perez,bperez7@dailymotion.com,200.10.5.0,Product Management,bperez7,lBSdzIds6QZ4
Billy,Ramirez,bramirez12@ameblo.jp,59.199.83.184,Engineering,bramirez12,9cLR8rgBR1vM
Brandon,Cook,bcook4@umn.edu,234.228.150.168,Marketing,bcook4,AN1xnOoEa
Brenda,Brown,bbrowng@squidoo.com,152.51.251.169,Human Resources,bbrowng,nobbp57gdbyr
Carol,Ramos,cramosi@amazon.co.jp,148.215.170.92,Business Development,cramosi,bPiECXyyttt
Carol,White,cwhiten@dot.gov,186.64.183.48,Engineering,cwhiten,GEGLcGC
Carolyn,Torres,ctorres1d@china.com.cn,200.10.5.255,Training,ctorres1d,H6MIi6#
Craig,Stanley,cstanley16@bluehost.com,132.26.39.116,Accounting,cstanley16,mY4Gxv@FB5m
Donna,Brooks,dbrooks6@reference.com,12.201.173.235,Accounting,dbrooks6,CCY2ROi
Earl,Bradley,ebradleyu@scribd.com,2.211.31.161,Research and Development,ebradleyu,6tPJtQzagXB
Earl,Peterson,epeterson19@bigcartel.com,80.151.104.248,Engineering,epeterson19,SZgcQviKSs
Edward,Jacobs,ejacobsf@liveinternet.ru,39.2.217.52,Services,ejacobsf,bnXT2chm
Elizabeth,Fields,efieldst@cmu.edu,200.10.5.150,Product Management,efieldst,J1ovlcLa6

使用regex有什么好的解决方案?

使用inet_aton(internet-alpha-to-number(从套接字更容易解决此问题。

from socket import inet_aton
import csv
start = inet_aton('200.10.5.1')
stop = inet_aton('254.0.0.0')
with open('f0.csv', 'r') as f:
reader = csv.DictReader(f)
for row in reader:
ip = row['ip_address']
if start <= inet_aton(ip) <= stop:
print(row)

对于您的样本数据,这将打印:

{'first_name': 'Brandon', 'last_name': 'Cook', 'email': 'bcook4@umn.edu', 'ip_address': '234.228.150.168', 'department': 'Marketing', 'username': 'bcook4', 'Password': 'AN1xnOoEa'}
{'first_name': 'Carolyn', 'last_name': 'Torres', 'email': 'ctorres1d@china.com.cn', 'ip_address': '200.10.5.255', 'department': 'Training', 'username': 'ctorres1d', 'Password': 'H6MIi6#'}
{'first_name': 'Elizabeth', 'last_name': 'Fields', 'email': 'efieldst@cmu.edu', 'ip_address': '200.10.5.150', 'department': 'Product Management', 'username': 'efieldst', 'Password': 'J1ovlcLa6'}

相关内容

  • 没有找到相关文章

最新更新