Python 实现在文件中的每一行添加一个逗号
2019/7/15 0:50:13
本文主要是介绍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)
以上这篇Python 实现在文件中的每一行添加一个逗号就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持找一找教程网。
这篇关于Python 实现在文件中的每一行添加一个逗号的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-08有遇到过吗?同样的规则 Excel 中 比Python 结果大
- 2024-03-30开始python成长之路
- 2024-03-29python optparse
- 2024-03-29python map 函数
- 2024-03-20invalid format specifier python
- 2024-03-18pool.map python
- 2024-03-18threads in python
- 2024-03-14python Ai 应用开发基础训练,字符串,字典,文件
- 2024-03-13id3 algorithm python
- 2024-03-13sum array elements python