简体   繁体   中英

Python script overwrite rows in Excel

I have the following code:

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()

The idea of this script is to export data in Excel from one file called users.log.

users.log looks like this:

--- 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

Code runs without error, but when it populates the column "1", it overwrites always the previous result. In column "1", "eq2" should start from the row where "eq1" finished +1. I think that the problem is with the appending to the contenthost list because for contenthost 1 I get list out of range and in contenthost[0] are listed both words. Any hint what it is wrong in the script?

UPDATE:

  • With the list outside of the loop 列表不在循环中

  • With the list inside of the loop 列表位于for循环内

Many thanks in advance.

将您的contenthost = []初始声明置于for循环之外,然后重试。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM