Python得到正确的行尾

标签 python line-endings

有没有一种简单的方法可以获取当前操作系统使用的行尾类型?

最佳答案

如果您正在对以文本模式打开的文件进行操作,那么您是正确的,换行符都显示为“\n”。否则,您正在寻找 os.linesep .

来自 http://docs.python.org/library/os.html :

os.linesep

The string used to separate (or, rather, terminate) lines on the current platform. This may be a single character, such as '\n' for POSIX, or multiple characters, for example, '\r\n' for Windows. 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.

关于Python得到正确的行尾,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/454725/

相关文章:

Excel for Mac 2016 - 另存为 CSV - 行尾错误

R read.table csv with classic-mac 行结尾

python - Cupy 无法找到 CUDA 存储库

python - 如何提供 SWIG 的包含路径?

python - 强制关闭后,Spyder(Anaconda)无法启动

Python3打印原始字节

python - 检查字典中的键是原子操作吗?

bash - 函数在 bash 脚本中不起作用 - 错误消息与 '\r' 相关

powershell - 如何保留 UNIX LF 行结尾?

c++ - 使用 istreambuf_iterator<char>(ifstream(..., ios::binary)) 进行奇怪的行结束转换(CR、LF、CRLF)