The following small series will bring you a python script to export data in Excel format (recommended ). I think this is quite good. now I will share it with you and give you a reference. Let's take a look at the small Editor. during my internship, a server engineer 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. it can automatically traverse all text files under a file directory and export the total data to an Excel file, export to Excel 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 above python script is a simple method (recommended) to export data in excel format, which is all the content shared by Alibaba Cloud. I hope to give you a reference and support for PHP.
For more articles about how to export data in excel format using python scripts, refer to PHP!