Python and php implement code for splitting files,

Source: Internet
Author: User

Python and php implement code for splitting files,

A friend said two days ago that he did not know how to implement a text file by dividing it into multiple text files by a fixed number of lines. If the data volume is small, you can manually split it. If the data volume is large, manual completion is too labor-intensive and unrealistic. Then we need to use scripts to implement it. Since a friend wants to simply complete this task, it is better to record it and provide convenience to those who need it.

Next, I will use python and php for script implementation and operations. Of course, it can be implemented in other languages. You can choose your own language based on your familiarity, if a friend has not yet achieved the ability to write code, then at least the use of the language environment, as long as you achieve this, you can complete the following work. The demo is as follows:

For example, we want to split the.txt file into multiple txt files based on five lines.

The A.txt file is the test data to be split. I have created 21 data records, as shown below:

Five txt files are separated by five lines. The python script is as follows: split. py.

Execute the Script: python split. py

View execution results:

Next, let's look at the 0.txtand 4.txt content:


In this way, 21 pieces of data are divided into 5 text files. In the case of a large amount of data, you only need to modify the LIMIT in the script (the number of lines to be split in a txt file ), of course, the file format is not limited to txt or csv files. If you are interested, try it.

The script written in php is as follows: it is not described here. We recommend that you use python.

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.