什么是os.linesep? [英] What is os.linesep for?

查看:296
本文介绍了什么是os.linesep?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python的os模块包含了一个特定于平台的字符串分隔字符串的值,但是当写入文件时,文档明确地说不使用它:


在编写以文本模式打开的文件(默认)时,不要使用os.linesep作为行结束符;请在所有平台上使用单一的\ n。

Google文档

以前的问题探讨了为什么你不应该在这种情况下使用它,但是什么上下文是有用的?什么时候应该使用行分隔符和什么?

解决方案


文档明确地说不要写入文件时使用它


这不是确切的,文档说不要在文本中使用它 mode。



os.linesep 用于遍历文本文件的行。内部扫描程序可识别 os.linesep ,并将其替换为一个\ n。



,我们编写了一个二进制文件,其中包含由\r\\\
(Windows分隔符)分隔的3行:

  import io 

filename =text.txt

content = b'line1\r\\\
line2\r\\\
line3'
with io.open (内容)

二进制文件的内容是:

 用io.open(filename,mode =rb)作为fd:$ 














$ >注意:我使用rb模式将文件读取为二进制文件。



我得到:

  b'line1 \r\''
b'line2 \r\''
b'line3'

如果我使用文本模式阅读文件的内容,如下所示:

  wit h:打印(repr(line))
的文件名,mode =r,encoding =ascii)为fd:



$ b

我得到:

$ p $ 'line1 \\\
'
'line2 \''
'line3'

分隔符被替换为\\\



在写入模式中也使用 os.linesep 任何\ n字符转换为系统默认行分隔符:Windows上的\r\\\
,POSIX上的\ n等。



<使用 io.open 函数,你可以强制任何你想要的行分隔符。



写一个Windows文本文件:

  with io.open(filename,mode =w,encoding =ascii,换行符=\r\\\
)为fd:
fd.write(one \\\
two\\\
three\\\


$ b

如果您以文本模式读取此文件,请执行以下操作:

  with io.open(文件名,模式=rb)为fd :
content = fd.read()
print(repr(content))



<你得到:

  b'one \r\\\
two\r\\\
three\r\\\
'


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:

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.

Docs

Previous questions have explored why you shouldn't use it in this context, but then what context is it useful for? When should you use the line separator, and for what?

解决方案

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 text mode.

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".

For illustration, we write a binary file which contains 3 lines separated by "\r\n" (Windows delimiter):

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.

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".

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.

With the io.open function you can force the line separator to whatever you want.

Example: how to write a Windows text file:

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'

这篇关于什么是os.linesep?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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