Python script to export data in excel format (recommended), python data export

Source: Internet
Author: User

Python script to export data in excel format (recommended), python data export

During my internship, a senior engineer from the server asked me to help sort out the server's log data. Finally, I used Python to extract the data and export it in Excel format.The following is the source code implemented by Python,You can automatically traverse all text files in a file directory, export the total data to an Excel file, and export the data to an Excel file to facilitate statistics.

// In reality, all the files in the directory in the format of .txt are input and output statistics. If it is another format, you can directly change the TXT file to the format suffix you need, which is more convenient.

// The process is to extract all the content from the file and write it into a new file, then extract data from the new file, and finally write the data into the Excel file.

From pyExcelerator import * import oscurrentpath = OS. getcwd () testlog = open ('test. mak', 'w') OS. mkdir (r 'excel ') print "currentpath:", currentpath for file in OS. listdir (currentpath): if OS. path. isfile (OS. path. join (currentpath, file) = True: if file.find('.txt ')> 0: // if a different format directly changes the following .txt file to the format suffix you need, you can set 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 (currentpath): 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 = margin I = 0for line in file_object: line = line. rstrip ('\ n') print 4if not line. split (): I = I + 1if line. strip (): array = line. split (':') lineleft = array [0] lineright = array [1] works. write (I, 0, lineleft) works. write (I, 1, lineright) I = I + Upload file = _ file. rstrip ('. log ') _ file = 'excel \ %s.xls' % _ filework. save (_ file)

// Print 1 2 3 4 is the log I typed. If you do not want it, you can delete it directly. This Python implementation directly saves the above Code to the file test. py.

In addition, the extracted Executable File log_parse.exe of a CEN folder is used in the middle and placed below. Put test. py in the same directory.

 
For convenience, you can create a. bat file and write it as a command line. Click it to automatically complete all the work, as shown below:

Echo
Python test. py

My own implementation is about MB of files ran for a minute and a half to get the results. I think it is still ideal.

The python script above is a simple method (recommended) to export data in excel format. It is a small part of the content that I share with you. I hope to give you a reference, and I hope you can provide more support for the customer.

Related Article

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.