我有以下代码:
import xlrd
import xlwt
import xlsxwriter
import openpyxl
import sys
import os
string0="Username"
stringhost="Host"
string1="Last password change"
string2="Password expires"
string3="Password inactive"
string4="Account expires"
string5="Minimum number of days between password change"
string6="Maximum number of days between password change"
string7="Number of days of warning before password expires"
hostid="---"
workbook=xlsxwriter.Workbook('/media/sf_vboxshared/sample.xlsx')
worksheet1=workbook.add_worksheet('Sheet1')
worksheet1.write(0,0,string0)
worksheet1.write(0,1,stringhost)
worksheet1.write(0,2,string1)
worksheet1.write(0,3,string2)
worksheet1.write(0,4,string3)
worksheet1.write(0,5,string4)
worksheet1.write(0,6,string5)
worksheet1.write(0,7,string6)
worksheet1.write(0,8,string7)
num1=1
num2=1
num3=1
num4=1
num5=1
num6=1
num7=1
num8=1
num9=1
contenttail=[]
with open("users.log",'r') as dump:
for num, line in enumerate(dump):
if string1 in line:
tail=line.split(string1)[1]
tail=tail.strip()
tail=tail.lstrip(":")
worksheet1.write(num1,2,tail)
num1=num1+1
elif string2 in line:
tail2=line.split(string2)[1]
tail2=tail2.strip()
tail2=tail2.lstrip(":")
worksheet1.write(num2,3,tail2)
num2=num2+1
elif string3 in line:
tail=line.split(string3)[1]
tail=tail.strip()
tail=tail.lstrip(":")
worksheet1.write(num3,4,tail)
num3=num3+1
elif string4 in line:
tail=line.split(string4)[1]
tail=tail.strip()
tail=tail.lstrip(":")
worksheet1.write(num4,5,tail)
num4=num4+1
elif string5 in line:
tail=line.split(string5)[1]
tail=tail.strip()
tail=tail.lstrip(":")
worksheet1.write(num5,6,tail)
num5=num5+1
elif string6 in line:
tail=line.split(string6)[1]
tail=tail.strip()
tail=tail.lstrip(":")
worksheet1.write(num6,7,tail)
num6=num6+1
elif string7 in line:
tail=line.split(string7)[1]
tail=tail.strip()
tail=tail.lstrip(":")
contenttail.append(tail)
worksheet1.write(num7,8,tail)
num7=num7+1
elif hostid in line:
words=line.split(hostid)[1]
print words
contenthost=[]
contenthost.append(words)
num9=1
for i in range(0,len(contenthost)):
for num9 in range(num9,len(contenttail)):
worksheet1.write(num9,1,contenthost[i])
num9=num9+len(contenttail)
content=[]
with open("users.log",'r') as dump:
for line in dump:
content.append(line)
for i in range(1, len(content),9):
worksheet1.write(num8,0,content[i])
num8=num8+1
workbook.close()
此脚本的想法是从一个名为users.log的文件中导出Excel中的数据。
users.log看起来像这样:
--- eq1
frank
Last password change : Mar 05, 2018
Password expires : never
Password inactive : never
Account expires : never
Minimum number of days between password change : 0
Maximum number of days between password change : 99999
Number of days of warning before password expires : 7
mark
Last password change : Aug 03, 2016
Password expires : never
Password inactive : never
Account expires : never
Minimum number of days between password change : 0
Maximum number of days between password change : 99999
Number of days of warning before password expires : 7
grand
Last password change : Aug 03, 2016
Password expires : never
Password inactive : never
Account expires : never
Minimum number of days between password change : 0
Maximum number of days between password change : 99999
Number of days of warning before password expires : 7
system
Last password change : Aug 03, 2016
Password expires : never
Password inactive : never
Account expires : never
Minimum number of days between password change : 0
Maximum number of days between password change : 99999
Number of days of warning before password expires : 7
syp
Last password change : Aug 03, 2016
Password expires : never
Password inactive : never
Account expires : never
Minimum number of days between password change : 0
Maximum number of days between password change : 99999
Number of days of warning before password expires : 7
games
Last password change : Aug 03, 2016
Password expires : never
Password inactive : never
Account expires : never
Minimum number of days between password change : 0
Maximum number of days between password change : 99999
Number of days of warning before password expires : 7
rob
Last password change : Aug 03, 2016
Password expires : never
Password inactive : never
Account expires : never
Minimum number of days between password change : 0
Maximum number of days between password change : 99999
Number of days of warning before password expires : 7
gpod
Last password change : Aug 03, 2016
Password expires : never
Password inactive : never
Account expires : never
Minimum number of days between password change : 0
Maximum number of days between password change : 99999
Number of days of warning before password expires : 7
gmail
Last password change : Aug 03, 2016
Password expires : never
Password inactive : never
Account expires : never
Minimum number of days between password change : 0
Maximum number of days between password change : 99999
Number of days of warning before password expires : 7
news
Last password change : Aug 03, 2016
Password expires : never
Password inactive : never
Account expires : never
Minimum number of days between password change : 0
Maximum number of days between password change : 99999
Number of days of warning before password expires : 7
qoop
Last password change : Aug 03, 2016
Password expires : never
Password inactive : never
Account expires : never
Minimum number of days between password change : 0
Maximum number of days between password change : 99999
Number of days of warning before password expires : 7
proxy
Last password change : Aug 03, 2016
Password expires : never
Password inactive : never
Account expires : never
Minimum number of days between password change : 0
Maximum number of days between password change : 99999
Number of days of warning before password expires : 7
data
Last password change : Aug 03, 2016
Password expires : never
Password inactive : never
Account expires : never
Minimum number of days between password change : 0
Maximum number of days between password change : 99999
Number of days of warning before password expires : 7
backup
Last password change : Aug 03, 2016
Password expires : never
Password inactive : never
Account expires : never
Minimum number of days between password change : 0
Maximum number of days between password change : 99999
Number of days of warning before password expires : 7
--- eq2
newlist
Last password change : Aug 03, 2016
Password expires : never
Password inactive : never
Account expires : never
Minimum number of days between password change : 0
Maximum number of days between password change : 99999
Number of days of warning before password expires : 7
dirk
Last password change : Aug 03, 2016
Password expires : never
Password inactive : never
Account expires : never
Minimum number of days between password change : 0
Maximum number of days between password change : 99999
Number of days of warning before password expires : 7
mas
Last password change : Aug 03, 2016
Password expires : never
Password inactive : never
Account expires : never
Minimum number of days between password change : 0
Maximum number of days between password change : 99999
Number of days of warning before password expires : 7
nobody
Last password change : Aug 03, 2016
Password expires : never
Password inactive : never
Account expires : never
Minimum number of days between password change : 0
Maximum number of days between password change : 99999
Number of days of warning before password expires : 7
row
Last password change : Aug 03, 2016
Password expires : never
Password inactive : never
Account expires : never
Minimum number of days between password change : 0
Maximum number of days between password change : 99999
Number of days of warning before password expires : 7
rag
Last password change : Aug 03, 2016
Password expires : never
Password inactive : never
Account expires : never
Minimum number of days between password change : 0
Maximum number of days between password change : 99999
Number of days of warning before password expires : 7
minibus
Last password change : Mar 05, 2018
Password expires : never
Password inactive : never
Account expires : never
Minimum number of days between password change : 0
Maximum number of days between password change : 99999
Number of days of warning before password expires : 7
cap
Last password change : Mar 05, 2018
Password expires : never
Password inactive : never
Account expires : never
Minimum number of days between password change : 0
Maximum number of days between password change : 99999
Number of days of warning before password expires : 7
greg
Last password change : Mar 05, 2018
Password expires : never
Password inactive : never
Account expires : never
Minimum number of days between password change : 0
Maximum number of days between password change : 99999
Number of days of warning before password expires : 7
vivid
Last password change : Mar 05, 2018
Password expires : never
Password inactive : never
Account expires : never
Minimum number of days between password change : 0
Maximum number of days between password change : 99999
Number of days of warning before password expires : 7
ntpd
Last password change : Mar 05, 2018
Password expires : never
Password inactive : never
Account expires : never
Minimum number of days between password change : 0
Maximum number of days between password change : 99999
Number of days of warning before password expires : 7
代码运行时没有错误,但是当它填充列“1”时,它会覆盖以前的结果。在列“1”中,“eq2”应从“eq1”完成+1的行开始。 我认为问题在于附加到contenthost列表,因为对于contenthost 1我得到列表超出范围并且在contenthost [0]中列出了两个单词。 任何暗示脚本中的错误是什么?
更新:
非常感谢提前。
答案 0 :(得分:0)
将contenthost = []
初始声明置于for
循环之外,然后重试。