Python os.linesep 有什么用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38074811/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is os.linesep for?
提问by SuperBiasedMan
Python's os module contains a value for a platform specific line separating string, but the docs explicitly say not to use it when writing to a file:
Python 的 os 模块包含一个特定于平台的行分隔字符串的值,但文档明确指出在写入文件时不要使用它:
Do not use os.linesep as a line terminator when writing files opened in text mode (the default); use a single '\n' instead, on all platforms.
在写入以文本模式(默认)打开的文件时,不要使用 os.linesep 作为行终止符;在所有平台上使用单个 '\n' 代替。
Previous questionshave explored why you shouldn'tuse it in this context, but then what context is it useful for? When should you use the line separator, and for what?
之前的问题已经探讨了为什么不应该在这种情况下使用它,但是它在什么情况下有用?什么时候应该使用行分隔符,用于什么?
采纳答案by Laurent LAPORTE
the docs explicitly say not to use it when writing to a file
文档明确说在写入文件时不要使用它
This is not exact, the doc says not to used it in textmode.
这并不准确,文档说不要在文本模式下使用它。
The os.linesep
is used when you want to iterate through the lines of a text file. The internal scanner recognise the os.linesep
and replace it by a single "\n".
在os.linesep
当你想通过一个文本文件的行进行迭代时使用。内部扫描器识别os.linesep
并用单个“\n”替换它。
For illustration, we write a binary file which contains 3 lines separated by "\r\n" (Windows delimiter):
为了说明,我们编写了一个二进制文件,其中包含 3 行,以“\r\n”(Windows 分隔符)分隔:
import io
filename = "text.txt"
content = b'line1\r\nline2\r\nline3'
with io.open(filename, mode="wb") as fd:
fd.write(content)
The content of the binary file is:
二进制文件的内容是:
with io.open(filename, mode="rb") as fd:
for line in fd:
print(repr(line))
NB: I used the "rb" mode to read the file as a binary file.
注意:我使用“rb”模式将文件作为二进制文件读取。
I get:
我得到:
b'line1\r\n'
b'line2\r\n'
b'line3'
If I read the content of the file using the text mode, like this:
如果我使用文本模式读取文件的内容,如下所示:
with io.open(filename, mode="r", encoding="ascii") as fd:
for line in fd:
print(repr(line))
I get:
我得到:
'line1\n'
'line2\n'
'line3'
The delimiter is replaced by "\n".
分隔符由“\n”代替。
The os.linesep
is also used in write mode: any "\n" character is converted to the system default line separator: "\r\n" on Windows, "\n" on POSIX, etc.
在os.linesep
也被用于在写模式下:任何“\ n”字符被转换成系统默认在线分离器:为“\ r \ n”个在Windows,“\ n”个上POSIX等
With the io.open
function you can force the line separator to whatever you want.
使用该io.open
功能,您可以将行分隔符强制为您想要的任何内容。
Example: how to write a Windows text file:
示例:如何编写 Windows 文本文件:
with io.open(filename, mode="w", encoding="ascii", newline="\r\n") as fd:
fd.write("one\ntwo\nthree\n")
If you read this file in text mode like this:
如果您像这样以文本模式阅读此文件:
with io.open(filename, mode="rb") as fd:
content = fd.read()
print(repr(content))
You get:
你得到:
b'one\r\ntwo\r\nthree\r\n'
回答by Rob
As you know, reading and writing files in text mode in python converts the platform specific line separator to '\n' and vice versa. But if you would read a file in binary mode, no conversion takes place. Then you can explicitly convert the line endings using string.replace(os.linesep, '\n')
. This can be useful if a file (or stream or whatever) contains a combination of binary and text data.
如您所知,在 python 中以文本模式读取和写入文件会将特定于平台的行分隔符转换为 '\n',反之亦然。但是,如果您以二进制模式读取文件,则不会发生转换。然后您可以使用string.replace(os.linesep, '\n')
. 如果文件(或流或其他)包含二进制和文本数据的组合,这会很有用。