我目前遇到下一个代码的错误:chain['xxx']=line.split()[10] IndexError: list index out of range
import xlsxwriter
word='Chain'
def create_chain(chain_segment):
chains=[]
chain_lines = [line for line in chain_segment.split('\n') if line]
for line in chain_lines:
chain={}
if word in line:
chain['type'] = line.split()[1]
elif line[0].isdigit():
chain['num']=line[0]
chain['pkts']=line.split()[1]
chain['bytes']=line.split()[2]
chain['target']=line.split()[3]
chain['prot']=line.split()[4]
chain['opt']=line.split()[5]
chain['in']=line.split()[6]
chain['out']=line.split()[7]
chain['source']=line.split()[8]
chain['destination']=line.split()[9]
chain['xxx']=line.split()[10]
chains.append(chain)
chains=filter(None, chains)
chains=list(chains)
chained = [merge_dicts(chains[0], i) for i in chains[1:]]
return chained
def merge_dicts(x,y):
z=x.copy()
z.update(y)
return z
with open('/media/sf_vboxshared/iptables-list.log') as f:
log_content = f.read()
host_sections = [host for host in log_content.split('---') if host]
hosts = {}
for host in host_sections:
hostname, chains_segment = host.split('\n', 1)
hostname = hostname.strip()
chains=[]
for segment in chains_segment.split('\n\n'):
chains.extend(create_chain(segment))
hosts[hostname] = chains
workbook=xlsxwriter.Workbook('/media/sf_vboxshared/iptables-1st.xlsx')
worksheet1=workbook.add_worksheet('Sheet1')
worksheet1.write(0,0,'hostname')
worksheet1.write(0,1,'chain')
worksheet1.write(0,2,'num')
worksheet1.write(0,3,'pkts')
worksheet1.write(0,4,'bytes')
worksheet1.write(0,5,'target')
worksheet1.write(0,6,'prot')
worksheet1.write(0,7,'opt')
worksheet1.write(0,8,'in')
worksheet1.write(0,9,'out')
worksheet1.write(0,10,'source')
worksheet1.write(0,11,'destination')
row = 1
for host, chains in hosts.items():
for chain in chains:
worksheet1.write(row, 1, chain.get('type'))
worksheet1.write(row, 0, host)
worksheet1.write(row, 2, chain.get('num'))
worksheet1.write(row, 3, chain.get('pkts'))
worksheet1.write(row, 4, chain.get('bytes'))
worksheet1.write(row, 5, chain.get('target'))
worksheet1.write(row, 6, chain.get('prot'))
worksheet1.write(row, 7, chain.get('opt'))
worksheet1.write(row, 8, chain.get('in'))
worksheet1.write(row, 9, chain.get('out'))
worksheet1.write(row, 10, chain.get('source'))
worksheet1.write(row, 11, chain.get('destination'))
worksheet1.write(row, 12, chain.get('xxx'))
row += 1
workbook.close()
在输入中,对于最后一列,某些行没有值。可以肯定的是,这个错误是由这些空白空间引起的,因为我已经用随机的东西填充它们并且它工作得很好。如果有空格,我需要以某种方式使代码忽略。
输入非常大,但这是文件的模式。
---node1
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
1 1 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0
2 25 16K ACCEPT udp -- * * 0.0.0.0/0 0.0.0.0/0
3 7 28 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:53
4 58 39K ACCEPT udp -- * * 0.0.0.0/0 0.0.0.0/0
5 81K 25M ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 ctstate RELATED,ESTABLISHED
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
1 0 0 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 ctstate RELATED,ESTABLISHED
2 0 0 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject
Chain OUTPUT (policy ACCEPT 398 packets, 23K bytes)
num pkts bytes target prot opt in out source destination
1 2K 3M ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 ctstate RELATED,ESTABLISHED
有没有办法实现这个目标? 附:我使用的是Python 2.7
非常感谢,
罗曼
答案 0 :(得分:2)
在输入中,对于最后一列,有些行没有 值。当然,这个错误是由这些空格引起的,因为 我已经随机填充了一些东西,它完美无缺。一世 如果有空,则需要以某种方式使代码忽略 空格。
你会明白,如果没有这样的索引,python就无法告诉你索引10的值。换句话说:你在一家餐馆,通常吃五道菜。服务员只给你四个,然后问你如何喜欢第五个。这个问题没有答案。
处理该问题的最pythonic方式是try
和except
:
# [...]
chain['source']=line.split()[8]
chain['destination']=line.split()[9]
try:
chain['xxx']=line.split()[10]
except IndexError:
pass # or do something else
事实上,上述解决方案被认为比检查line
的长度更好,如果不够,则不允许进入第[10]项。