Member 13815190 Ответов: 0

Питон автоматизация данных в Excel


I am currently trying to export data from some log files into Excel. Export of the data should be like this: 
•For each file should be created a new sheet with the name of the file 
•In each sheet should be just the data from the one log file

I have tried something like this and I feel that I am quite close, but I get the data mixed in the Excel sheet over 6 sheets. In the /home/dante/sample/sample2/ folder where I have the Python script, I have also the log files, but nothing else. 

One of the log files looks like this: 
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

Any idea how can be this corrected? 

Many thanks in advance, Dan


Что я уже пробовал:

import xlrd
import xlwt
import xlsxwriter
import sys
import os

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-auto.xlsx')

for filenames in os.listdir("/home/dante/sample/sample2/"):
   with open(filenames,'r') as dump:
     for i in range(1,len(os.listdir("/home/dante/sample/sample2/"))):
       worksheet=workbook.add_worksheet()
       for num, line in enumerate(dump):
           if string1 in line: 
              tail=line.split(string1)[1]
              tail=tail.strip()
              tail=tail.lstrip(":")      
              worksheet.write(num,0,string1)
              worksheet.write(num,1,tail)
           elif string2 in line:
              tail2=line.split(string2)[1]
              tail2=tail2.strip()
              tail2=tail2.lstrip(":")
              worksheet.write(num,0,string2)
              worksheet.write(num,1,tail2)
           elif string3 in line:
              tail=line.split(string3)[1]
              tail=tail.strip()
              tail=tail.lstrip(":")
              worksheet.write(num,0,string3)
              worksheet.write(num,1,tail)
           elif string4 in line:
              tail=line.split(string4)[1]
              tail=tail.strip()
              tail=tail.lstrip(":")
              worksheet.write(num,0,string4)
              worksheet.write(num,1,tail)
           elif string5 in line:
              tail=line.split(string5)[1]
              tail=tail.strip()
              tail=tail.lstrip(":")
              worksheet.write(num,0,string5)
              worksheet.write(num,1,tail)
           elif string6 in line:
              tail=line.split(string6)[1]
              tail=tail.strip()
              tail=tail.lstrip(":")
              worksheet.write(num,0,string6)
              worksheet.write(num,1,tail)
           elif string7 in line:
              tail=line.split(string7)[1]
              tail=tail.strip()
              tail=tail.lstrip(":")
              worksheet.write(num,0,string7)
              worksheet.write(num,1,tail)
       content=[]
   with open(filenames,'r') as dump:
           for line in dump:
              content.append(line)
           for i in range(1, len(content),9):
              worksheet.write(i,0,content[i]) 
workbook.close()

0 Ответов