A simple way for data to export Excel format with Python script

Source: Internet
Author: User
During the internship, a brother of the server asked me to help tidy up the log data of the server, and finally I used Python to extract the data and export it in Excel format. Below is my python implementation of the source code, you can automatically traverse a file directory of all text files, and the total data exported to Excel file, exported to Excel format so it is more convenient statistics.

Implementation of the directory of all files formatted as. txt file to traverse the statistics, if it is another format directly to the following. txt to the format you need to suffix it, more convenient.

The process is to extract all the contents of the file into a new file, then extract the data from the new file, and finally write the data to the Excel file

From pyexcelerator import *import Oscurrentpath = OS.GETCWD () testlog = open (' Test.mak ', ' W ') Os.mkdir (R ' Excel ') print "cur RentPath: ", Currentpath for file in Os.listdir (Currentpath): If Os.path.isfile (Os.path.join (currentpath,file)) ==true: If File.find ('. txt ') >0://If it is a different format, simply change the following. txt to the format suffix you want, file_ = open (file, ' r ') content = File_.read () file_.close () testlog.write (content) Print 1os.popen (' Log_parse.exe test.mak >> shuju.log ') print 2for _file in Os.listdir (cu Rrentpath): If Os.path.isfile (Os.path.join (currentpath,_file)) ==true:if _file.find ('. log ') >0:work = Workbook () Works = Work.add_sheet (' Sheet1 ') Print 3file_object = open (_file) for I in Range (0,2): Works.col (i). width = 10000i = 0for Li NE in file_object:line = line.rstrip (' \ n ') print 4if not Line.split (): i = i + 1if line.strip (): array = line.split (': ') linel EFT = Array[0]lineright = Array[1]works.write (i,0,lineleft) works.write (i,1,lineright) i = i + 1_file = _file.rstrip ('. Log ') _file = ' Excel\%s.xls '% _filework.save (_fIle 

One of the print 1 2 3 4 is my log if you do not want to be able to delete directly. With this Python implementation, you can simply save the above code to the test.py file.

In the middle of the use of a C + + extract executable file Log_parse.exe, placed below. You can use it with test.py in the same directory.


If you want to be convenient, you can build a. bat file in the form of a command line, you can do all the work automatically with a single click, as follows:

Echo
Python test.py

My own implementation is about 150M files run a minute and a half of the time out of the results, I think it is more ideal.

Above this Python script implementation of data export Excel format simple method (recommended) is a small part of the whole content to share to everyone, I hope to give you a reference, but also hope that we have a lot of support topic.alibabacloud.com.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.