我有一个列表列表,我想按字符串“名称”和“地址”来匹配每个列表中的项目,我想将匹配的项目导出到Excel中
['Server: Corp', 'Address: 10.17.2.5\r', '\r', 'Name: b.resolvers.level3.net\r', 'Address: 4.2.2.2\r', '\r', ''],
['Server: Corp', 'Address: 10.17.2.5\r', '\r', 'Name: google-public-dns-a.google.com\r', 'Address: 8.8.8.8\r', '\r', ''],
['Server: Corp', 'Address: 10.17.2.5\r', '\r', 'Name: dns.quad9.net\r', 'Address: 9.9.9.9\r', '\r', '']
import re
m = re.search(r'\bName\b'| \bAddress\b', line)
for line in output:
if m:
print(m.group())
我想写的东西:
b.resolvers.level3.net , 4.2.2.2
google-public-dns-a.google.com , 8.8.8.8
dns.quad9.net , 9.9.9.9
答案 0 :(得分:0)
尝试
IsRecording
lists=[['Server: Corp', 'Address: 10.17.2.5\r', '\r', 'Name: b.resolvers.level3.net\r', 'Address: 4.2.2.2\r', '\r', ''],['Server: Corp', 'Address: 10.17.2.5\r', '\r', 'Name: google-public-dns-a.google.com\r', 'Address: 8.8.8.8\r', '\r', ''],
['Server: Corp', 'Address: 10.17.2.5\r', '\r', 'Name: dns.quad9.net\r', 'Address: 9.9.9.9\r', '\r', '']]
list2=[]
for singleList in lists:
list1=[]
flag=0
for item in singleList:
matcher=re.search('Name:(.*)', item)
if matcher:
list1.append(matcher.group(1).strip())
print(matcher.group(1).strip())
flag=1
if flag==1:
matcher=re.search('Address:(.*)', item)
if matcher:
list1.append(matcher.group(1).strip())
print(matcher.group(1).strip())
list2.append(list1)
import xlwt
from tempfile import TemporaryFile
book = xlwt.Workbook()
sheet1 = book.add_sheet('sheet1')
for j,a in enumerate(list2):
for i,e in enumerate(a):
sheet1.write(j,i,e)
name = "random.xls"
book.save(name)
book.save(TemporaryFile())
将成为列表列表。 list2包含
lists
可以导出到excel或csv