簡體   English   中英

Python在Excel中覆蓋數據

[英]Python overwriting data in Excel

我不是每天都在使用Python,並且遇到一個錯誤,您可能會幫助我。 我有以下代碼:

import xlrd
import xlwt
import xlsxwriter

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"

workbook=xlsxwriter.Workbook('/media/sf_vboxshared/sample.xlsx')
worksheet1=workbook.add_worksheet('Sheet1')

with open('sample.txt','r') as dump:
   for line in dump:
       if string1 in line:
            tail1=line.split(string1)[1]
            tail1=tail1.strip()
            tail1=tail1.lstrip(":")
 for num, line in enumerate(dump, 1):
                if string1 in line:
                     worksheet1.write(num,1,string1)
                     worksheet1.write(num,2,tail1)
with open('sample.txt', 'r') as dump2:
   for line2 in dump2:
       if string2 in line2:
            tail2=line2.split(string2)[1]
            tail2=tail2.strip()
            tail2=tail2.lstrip(":")
            for num, line2 in enumerate(dump2, 1):
                if string2 in line2:
                     worksheet1.write(num,1,string2)
                     worksheet1.write(num,2,tail2)

workbook.close()

輸入看起來像這樣(sample.txt):

rick
Last password change                                    : Feb 26, 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

john
Last password change                                    : Feb 26, 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

frank
Last password change                                    : Feb 26, 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

romeo
Last password change                                    : Feb 26, 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

這段代碼的想法是,我想將確切的選項卡從sample.txt文件導出到Excel,但格式正確。 基本上,:分隔列。 當您運行此代碼時,第二個“ with”將覆蓋信息。 有人可以向我解釋為什么這是這種現象,如何解決?

預先感謝Romain。

您遇到的問題是由Python讀取文件的方式引起的。 如果您打開文件並開始讀取文件,Python將會在光標位置返回數據(第一次打開文件時光標將位於0,因此文件的開頭)。 逐行讀取將導致光標每次跳至下一行:

>>> with open('sample.txt','r') as dump:
    print dump.readline()
    print dump.readline()

將返回:

rick
Last password change                                    : Feb 24, 2018

在您的示例中,您打開文件並開始逐行讀取文件:

with open('sample.txt', 'r') as dump2:
   for line2 in dump2:

直到您與if string2 in line2:匹配為止。 此時,您將開始enumerate文件的其余部分,但該光標已經移動,因此計數器與行的實際編號不匹配。

嘗試這樣的事情:

with open('sample.txt','r') as dump:
    for num, line in enumerate(dump):
        if string1 in line:
            tail1=line.split(string1)[1]
            tail1=tail1.strip()
            tail1=tail1.lstrip(":")
            worksheet1.write(num,1,string1)
            worksheet1.write(num,2,tail1)
        elif string2 in line:
            tail2=line.split(string2)[1]
            tail2=tail2.strip()
            tail2=tail2.lstrip(":")
            worksheet1.write(num,1,string2)
            worksheet1.write(num,2,tail2)

xslx文件的外觀如下 (rick的上次密碼更改在第2行中,密碼有效期在第3行中,等等)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM