gpt4 book ai didi

Python 将回车解码为换行

转载 作者:太空宇宙 更新时间:2023-11-03 13:07:01 25 4
gpt4 key购买 nike

我正在努力寻找关于为什么 python 将回车解码为换行的解释。

我这里有一个包含回车符 (\r) 的单行文本文件示例。如果我将它作为指定编码的文本文件打开,我找不到与以二进制模式打开文件并使用相同编码对其进行解码时相同的字符串。

确实,当我以文本模式打开文件时,CR 字符被替换为换行符 (\n)。

为什么?

with open("text.txt", encoding="utf-8", mode="r") as f:
txt = f.read()
txt
>>>'4312;"blabla";";\n";"blabla2"\n'

with open("text.txt", mode="rb") as f:
txt2 = f.read()
txt2.decode("utf-8")
>>>'4312;"blabla";";\r";"blabla2"\n'

最佳答案

这是因为 open 函数的通用换行模式,默认启用,自动规范化 '\r''\r\n' 进入 '\n'。如果您希望保留原始行结尾,则可以使用 newline='' 参数:

with open("text.txt", encoding="utf-8", mode="r", newline='') as f:

摘自 documentation :

newline controls how universal newlines works (it only applies to text mode). It can be None, '', '\n', '\r', and '\r\n'. It works as follows:

  • On input, if newline is None, universal newlines mode is enabled. Lines in the input can end in '\n', '\r', or '\r\n', and these are translated into '\n' before being returned to the caller. If it is '', universal newline mode is enabled, but line endings are returned to the caller untranslated. If it has any of the other legal values, input lines are only terminated by the given string, and the line ending is returned to the caller untranslated.
  • On output, if newline is None, any '\n' characters written are translated to the system default line separator, os.linesep. If newline is '', no translation takes place. If newline is any of the other legal values, any '\n' characters written are translated to the given string.

关于Python 将回车解码为换行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55184996/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com