


During the internship, a senior fellow on 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 of my Python implementation, which can automatically traverse all text files in a certain file directory and export the total data to an Excel file. Exporting it to Excel format is more convenient for statistics.
//Achieve traversal and statistics of all files in the directory in the .txt format. If it is in another format, just change the following .txt to the format suffix you need, which is more convenient.
//The process is to first extract the contents of all files and write them into a new file, then extract the data from the new file, and finally write the data into the Excel file
from pyExcelerator import * import os currentpath = 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: //如果是别的格式直接将下面的.txt改为你所需要的格式后缀就可以了 file_ = open(file,'r') content = file_.read() file_.close() testlog.write( content ) print 1 os.popen('log_parse.exe test.mak >> shuju.log') print 2 for _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 3 file_object = open(_file) for i in range(0,2): works.col(i).width = 10000 i = 0 for line in file_object: line = line.rstrip('\n') print 4 if not line.split(): i = i + 1 if line.strip(): array = line.split(':') lineleft = 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' % _file work.save(_file)
//The print 1 2 3 4 is the log I made. If you don’t want it, you can delete it directly. When using this Python implementation, just save the above code directly to the test.py file.
In addition, a c++ extraction executable file log_parse.exe is used in the middle, which is placed below. When using it, just put it in the same directory as test.py.
If you want to be more convenient, you can create a .bat file and write it in the form of a command line. You can automatically complete all the work with just one click, as follows:
echo
python test.py
My own implementation is to run about 150M files and get the result in one and a half minutes, which I think is quite ideal.
The simple method (recommended) of the above python script to export data to excel format is all the content shared by the editor. I hope it can give you a reference, and I hope you will support the PHP Chinese website.
For more python scripts to implement simple methods for exporting data to excel format, please pay attention to the PHP Chinese website for related articles!

The basic syntax for Python list slicing is list[start:stop:step]. 1.start is the first element index included, 2.stop is the first element index excluded, and 3.step determines the step size between elements. Slices are not only used to extract data, but also to modify and invert lists.

Listsoutperformarraysin:1)dynamicsizingandfrequentinsertions/deletions,2)storingheterogeneousdata,and3)memoryefficiencyforsparsedata,butmayhaveslightperformancecostsincertainoperations.

ToconvertaPythonarraytoalist,usethelist()constructororageneratorexpression.1)Importthearraymoduleandcreateanarray.2)Uselist(arr)or[xforxinarr]toconvertittoalist,consideringperformanceandmemoryefficiencyforlargedatasets.

ChoosearraysoverlistsinPythonforbetterperformanceandmemoryefficiencyinspecificscenarios.1)Largenumericaldatasets:Arraysreducememoryusage.2)Performance-criticaloperations:Arraysofferspeedboostsfortaskslikeappendingorsearching.3)Typesafety:Arraysenforc

In Python, you can use for loops, enumerate and list comprehensions to traverse lists; in Java, you can use traditional for loops and enhanced for loops to traverse arrays. 1. Python list traversal methods include: for loop, enumerate and list comprehension. 2. Java array traversal methods include: traditional for loop and enhanced for loop.

The article discusses Python's new "match" statement introduced in version 3.10, which serves as an equivalent to switch statements in other languages. It enhances code readability and offers performance benefits over traditional if-elif-el

Exception Groups in Python 3.11 allow handling multiple exceptions simultaneously, improving error management in concurrent scenarios and complex operations.

Function annotations in Python add metadata to functions for type checking, documentation, and IDE support. They enhance code readability, maintenance, and are crucial in API development, data science, and library creation.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Atom editor mac version download
The most popular open source editor

SublimeText3 Chinese version
Chinese version, very easy to use

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment
