Python - 替换文件输出中的换行符为回车/换行符

3

首先,我只学习了几个月的Python编程技术。

其次,我有一个需要处理的txt文件。为了正确处理该文件,每一行必须以'\r\n'结尾。

然而,并不总是这种情况,有时候它只包含'\n'(换行符)。

因此,我需要检查文件,如果缺少'\r\n',则用'\r\n'替换'\n'。

我已经尝试了几种方法,但都失败了。

首先我尝试了以下方法:

with open("initial_file.txt",'rb') as file_content:
  #1253 is a Windows code page used to write modern Greek.
  mycon = file_content.read().decode('cp1253')

if (mycon.count("\r\n") == 0) and (mycon.count("\n") > 0):
  with open("destination_file.txt",'w') as file_replace:
     file_replace.write(mycon.replace("\n", "\r\n").encode('cp1253'))

但是我替换'\n' 为 '\r\n' 时却得到了 '\r\r\n'。

因此,我尝试了另一种方法:

rf = open("initial_file.txt", 'rb')
wf = open("destination_file.txt",'wb')
mycon = rf.read().decode('cp1253')
if (mycon.count('\r\n') == 0) and (mycon.count('\n') > 0):
    for line in rf:
        newline = line.rstrip('\n')
        wf.write(newline).encode('cp1253')
        wf.write('\r\n').encode('cp1253') 

第一次可以正常使用,但之后就不行了。

我不确定自己哪里出错了,希望能得到一些帮助。

3个回答

3
没有编码转换,
with open("file.txt") as rf, open("out.txt", "w") as wf:
    for line in rf:
        wf.write("%s\r\n" % line.strip())

上下文管理器很酷,不是吗。


我的第一个代码示例也有同样的问题。出于某种原因,我无法理解它将 '\n' 替换为 '\r\r\n'。 - Giota B
1
如果您不确定文件是否有回车符,那么这个答案是可行的,非常棒。 - rjurney

2
你可以使用 open 函数从 io 模块 中。在那里,你可以明确指定换行模式。在这个例子中,每个 '\n' 都会被转换为 '\r\n'
from io import open
with open("test.txt", "w+", newline="\r\n") as f:
    f.write(u"Hello World\n")

以下是文件内容(0d 0a 对应 \r\n):

$ hexdump -C test.txt
00000000  48 65 6c 6c 6f 20 57 6f  72 6c 64 0d 0a           |Hello World..|

如果要将现有文件转换为使用特定的行终止模式,您可以利用 通用换行符 方法读取行。然后,您可以使用明确指定的行终止符写入输出文件中的行。

from io import open
with open(infname, 'r') as inf, open(outfname, "w+", newline="\r\n") as outf:
    outf.writelines(inf)

供参考:


谢谢,非常有效!我会确保阅读你提供的所有参考资料。 - Giota B

1
你正在以二进制模式读取输入,但以文本模式写入输出。文本模式会自动将换行符转换为特定于平台的换行序列。由于CP1253与ascii和unicode具有相同的换行符和回车符,因此可以完全删除字符串替换代码和编码/解码代码。如果不是这种情况,您可能需要以二进制模式打开输出或进行其他更改。
with open("file.txt", 'rb') as rf, open("out.txt", "w") as wf:
    wf.writelines(rf)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接