脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - Python 实现在文件中的每一行添加一个逗号

Python 实现在文件中的每一行添加一个逗号

2021-02-08 00:29DeniuHe Python

下面小编就为大家分享一篇Python 实现在文件中的每一行添加一个逗号,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

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

步骤2:对每行列表化

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

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

代码实现如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#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)

以上这篇Python 实现在文件中的每一行添加一个逗号就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/DeniuHe/article/details/77130858

延伸 · 阅读

精彩推荐