Python reads files and implements file-related operations last exported Excel

Source: Internet
Author: User

Today made a use of Python to extract the contents of the log, and exported Excel small script, the code is not good, novice do not spray, the following to summarize.

The requirements are as follows:

1. Read the number of file lines and make an Excel table

2. Read the 7th field in the file, summing up the sum to find the average. And find out the biggest of them. Do Excel tables.

3. Find out the number of occurrences of specific characters in the file, statistics, and do Excel tables.

4. Automatically generated Excel name is called Real time.


Read the number of file lines The first idea is to wc-l, although Python can invoke shell commands, but it is not very good, so here with Python to read the file line by row and accumulate, calculate the number of file lines ... Maybe a little silly, but I'm on the same level right now. For reading the seventh field of a file, I just read the file by line and split each line. Note that after split here is a list, each element of the list is a string, and I want to do an additive operation, so cast to int. Because the use of Python is not long, in solving this problem, the first idea or Python calls awk or cut to solve the problem ... It seems that you need to change your mind. And the following to find out the specific character of the problem this thinking is more serious, because everyone knows how easy grep, I still use a bit of Python, after each row split, select a specific field to determine whether this character, after the counter accumulated ... Maybe it's a little silly ....

Next talk about making Excel table, this is occasionally from the Liu Dashin of a book to see, so here laugh use, are routines, code will have.

The acquisition of system time is a single line of code.

Here by the way, when making Excel table, choose which range of data to make a table is required, you should only select one row or column, but not a diagonal selection of an area, if you want a table of multiple sets of data, it will have to load the data multiple times, each load a group, so there will be no problem. Otherwise it will be reported to see the wrong ....


This article is from the "Learning discussion" blog, so be sure to keep this source http://ltm250.blog.51cto.com/4695970/1613415

Python reads files and implements file-related operations last exported Excel

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.