Home > Backend Development > Python Tutorial > Python implementation adds a comma to each line in the file

Python implementation adds a comma to each line in the file

不言
Release: 2018-05-02 16:11:42
Original
2902 people have browsed it

这篇文章主要介绍了关于Python 实现在文件中的每一行添加一个逗号,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下

步骤1:读取每行(每行的类型是str)

步骤2:对每行列表化

步骤3:弹出每行的/n两个字符

步骤4:追加,/n三个字符

代码实现如下:

#import os
From_file=open('D:\\python\\A\\tianqi.txt')
f=open('niuniu1.txt','w')
count=0
huancun=[]
for each_line in From_file:
 #print(type(each_line)) each_line 是字符类型
 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('文件中总共有:%d行'%count)
Copy after login

相关推荐:

python 实现在txt指定行追加文本的方法

Python 实现字符串中指定位置插入一个字符

The above is the detailed content of Python implementation adds a comma to each line in the file. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template