Python 写替换“\n"用“\r\n"在 Windows 中 [英] Python Write Replaces "\n" With "\r\n" in Windows

查看:51
本文介绍了Python 写替换“\n"用“\r\n"在 Windows 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在查看我的问题here后,我发现它是由通过一个更简单的问题.

After looking into my question here, I found that it was caused by a simpler problem.

当我将 "\n" 写入文件时,我希望从文件中读取 "\n" .在 Windows 中并非总是如此.

When I write "\n" to a file, I expect to read in "\n" from the file. This is not always the case in Windows.

In [1]: with open("out", "w") as file:
   ...:     file.write("\n")
   ...:

In [2]: with open("out", "r") as file:
   ...:     s = file.read()
   ...:

In [3]: s  # I expect "\n" and I get it
Out[3]: '\n'

In [4]: with open("out", "rb") as file:
   ...:     b = file.read()
   ...:

In [5]: b  # I expect b"\n"... Uh-oh
Out[5]: b'\r\n'

In [6]: with open("out", "wb") as file:
   ...:     file.write(b"\n")
   ...:

In [7]: with open("out", "r") as file:
   ...:     s = file.read()
   ...:

In [8]: s  # I expect "\n" and I get it
Out[8]: '\n'

In [9]: with open("out", "rb") as file:
   ...:     b = file.read()
   ...:

In [10]: b  # I expect b"\n" and I get it
Out[10]: b'\n'

更有条理:

| Method of Writing | Method of Reading | "\n" Turns Into |
|-------------------|-------------------|-----------------|
| "w"               | "r"               | "\n"            |
| "w"               | "rb"              | b"\r\n"         |
| "wb"              | "r"               | "\n"            |
| "wb"              | "rb"              | b"\n"           |

当我在我的 Linux 虚拟机上尝试这个时,它总是返回 \n.如何在 Windows 中执行此操作?

When I try this on my Linux virtual machine, it always returns \n. How can I do this in Windows?

这对于 Pandas 库来说尤其成问题,它似乎将 DataFrame s 写入 csv"w" 并读取 csvs 与 "rb".有关此示例,请参阅顶部链接的问题.

This is especially problematic with the pandas library, which appears to write DataFrames to csv with "w" and read csvs with "rb". See the question linked at the top for an example of this.

推荐答案

既然您使用的是 Python 3,那么您很幸运.打开文件进行写入时,只需指定newline='\n' 以确保写入的是'\n' 而不是系统默认的\r\n 在 Windows 上.来自文档:

Since you are using Python 3, you're in luck. When you open the file for writing, just specify newline='\n' to ensure that it writes '\n' instead of the system default, which is \r\n on Windows. From the docs:

将输出写入流时,如果newlineNone,则写入的任何'\n' 字符都将转换为系统默认行分隔符,os.linesep.如果换行符是 '''\n',则不会进行转换.如果 newline 是任何其他合法值,则写入的任何 '\n' 字符都将转换为给定的字符串.

When writing output to the stream, if newline is None, any '\n' characters written are translated to the system default line separator, os.linesep. If newline is '' or '\n', no translation takes place. If newline is any of the other legal values, any '\n' characters written are translated to the given string.

您认为有时"会看到两个字符输出的原因是,当您以二进制模式打开文件时,根本没有进行任何转换.为方便起见,字节数组仅以 ASCII 显示.在解码之前不要将它们视为真正的字符串.您显示的二进制输出是所有示例中文件的真实内容.

The reason that you think that you are "sometimes" seeing the two-character output is that when you open the file in binary mode, no conversion is done at all. Byte arrays are just displayed in ASCII whenever possible for your convenience. Don't think of them as real strings until they have been decoded. The binary output you show is the true contents of the file in all your examples.

当您以默认文本模式打开文件进行读取时,newline 参数的工作方式与写入时的工作方式类似.默认情况下,文件中的所有 \r\n 将在字符解码后转换为 \n.当您的代码在操作系统之间传输但您的文件不传输时,这非常好,因为您可以使用仅依赖于 \n 的完全相同的代码.如果您的文件也需要移动,您应该至少在输出方面坚持使用相对便携的 newline='\n'.

When you open the file for reading in the default text mode, the newline parameter will work similarly to how it does for writing. By default all \r\n in the file will be converted to just \n after the characters are decoded. This is very nice when your code travels between OSes but your files do not since you can use the exact same code that relies only on \n. If your files travel too, you should stick to the relatively portable newline='\n' for at least the output.

这篇关于Python 写替换“\n"用“\r\n"在 Windows 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆