Python implementation adds a comma to each line in the file

Source: Internet
Author: User
This article mainly describes the Python implementation in the file each line to add a comma, has a certain reference value, now share to everyone, the need for friends can refer to

Step 1: read each line (the type of each line is str)

Step 2: list each row

Step 3: eject each line of/n two characters

Step 4: Append,/n three characters

The code is implemented as follows:

#import osfrom_file=open (' D:\\python\\a\\tianqi.txt ') f=open (' Niuniu1.txt ', ' W ') count=0huancun=[]for Each_line in From_file: #print (Type (each_line)) Each_line is a character type delstr=list (Each_line) delstr.pop () #弹出n delstr.pop () #弹出 \ Delstr.append (', \ n ') huancun= "". Join (DELSTR) print (Huancun) f.writelines (huancun) count+=1 huancun=[]f.close () From_ File.close () print (' Total file:%d lines '%count)

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.