Python implementation: Split a text file into multiple small text files (PHP can also be implemented)

Source: Internet
Author: User
Tags python script

Two days ago a friend said, want to implement a text file according to a fixed number of lines to split into multiple text files, but do not know how to achieve. If the amount of data is small manual segmentation is good, if the volume of data is very large, manual completion is too labor-intensive, also unrealistic. Then you need to use scripts to implement them. Since a friend wants to accomplish this task simply, it is better to record it and make it easier for a friend in need.

The following I use Python and PHP for the implementation and operation of the script, of course, in other languages can be achieved, you can according to the language of the familiarity of the independent choice, if a friend has not reached the ability to write code, then at least the use of the language environment will be, as long as the attainment of these, You can do the following work. The demo is as follows:

For example, we want to split the A.txt file into multiple txt files, separated by 5 lines.

A.txt file for the test data to be split, I created 21 data, as follows:

Each of the five lines is divided into 5 txt files, the following is the implementation of the Python script: split.py

Execute script: Python split.py

To view the execution results:

Look at the contents of 0.txt and 4.txt:


In this way 21 data is divided into 5 text files, the same data volume, only need to modify the script limit (a TXT file to split the number of bars), of course, the file format is not limited to txt,csv and so on, interested friends may wish to try.

The script written in PHP is as follows: It's not too much to describe here, and it's recommended to use Python.

Python implementation: Split a text file into multiple small text files (PHP can also be implemented)

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.