Python divides the text into two lines and saves them to the file. python has two lines.

Source: Internet
Author: User

Python divides the text into two lines and saves them to the file. python has two lines.

Business Requirements

You need to divide the text file into a group of two lines.

Jb51.txt

1: www.jb51.net
2: www.jb51.net
3: www.jb51.net
4: www.jb51.net
5: www.jb51.net
6: www.jb51.net
7: www.jb51.net
8: www.jb51.net
9: www.jb51.net
10: www.jb51.net
11: www.jb51.net
12: www.jb51.net
13: www.jb51.net
14: www.jb51.net
15: www.jb51.net
16: www.jb51.net

Core code:

#-*-Coding: UTF-8-*-'''python reads files. Each of the two behaviors is a set of ''' def fenhang (infile, outfile): infopen = open (infile, 'R', encoding = 'utf-8') outopen = open (outfile, 'w', encoding = 'utf-8') lines = infopen. readlines () I = 1 for line in lines: if I % 2 = 0: outopen. write (line + '\ n') else: outopen. write (line) I + = 1 infopen. close () outopen. close () fenhang ("jb51.txt", "o.txt ")

RunPython jb51.txt

O.txt content

This article has been completed. I hope you will be able to support the customer's home in the future.

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.