我不是每天都在使用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”将覆盖该信息。有人可以解释我为什么这种行为以及如何解决?
非常感谢, 罗曼。
答案 0 :(得分:1)
您遇到的问题是由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)
Here is how the xslx file looks like (rick的最后一次密码更改在第2行,第3行的密码到期日期等...)