参考文章
python 操作 txt 文件中数据教程[1]-使用 python 读写 txt 文件[1]
python 操作 txt 文件中数据教程[2]-python 提取 txt 文件中的行列元素[2]
python 操作 txt 文件中数据教程[3]-python 读取文件夹中所有 txt 文件并将数据转为 csv 文件[3]
open(filename, 'r')
读取原始文件内容,再使用open(fileneme, 'w')
将删除了行尾回车符的字符串写入到新的文件中。即要做 读写分离 。filename = "./text.txt"
with open(filename, 'r') as f:
print("open OK")
for line in f.readlines():
for a in line:
# print(a)
if a == '\n':
print("This is \\n")
a = " "
for line in f.readlines():
for a in line:
if a == '\n':
print("This is \\r\\n")
for line in f.readlines():
line = line.replace("\n", " ")
line = line.strip("\n")
"""open OK
This is \n
This is \n
This is \n
This is \n
This is \n
This is \n
This is \n
This is \n
This is \n
This is \n
This is \n
This is \n
This is \n
This is \n
This is \n
This is \n
"""
with open('./text_1.txt', 'w') as f:
with open('./text.txt', 'r') as fp:
for line in fp:
line = str(line).replace("\n", " ")
f.write(line)
[1]python操作txt文件中数据教程[1]-使用python读写txt文件: https://blog.csdn.net/u013555719/article/details/84553722
[2]python操作txt文件中数据教程[2]-python提取txt文件中的行列元素: https://blog.csdn.net/u013555719/article/details/84554355
[3]python操作txt文件中数据教程[3]-python读取文件夹中所有txt文件并将数据转为csv文件: https://blog.csdn.net/u013555719/article/details/84554568