简体   繁体   English

Python的ConfigParser:跨平台的行尾吗?

[英]Python's ConfigParser: Cross platform line endings?

Does anyone know how Python deals with ConfigParser line endings in the different OSes? 有谁知道Python如何处理不同OS中的ConfigParser行尾? Because it follows the Windows INI format. 因为它遵循Windows INI格式。 But what about Linux? 但是Linux呢?

(As you know, Windows text line endings are typically CRLF, and Unix's are CR.) (您知道,Windows文本行的结尾通常是CRLF,而Unix的结尾是CR。)

I want users of my app to take their config files (.INI files) easily from Windows to Linux and I'd like to know if that's going to be problematic. 我希望我的应用程序的用户可以轻松地将配置文件(.INI文件)从Windows迁移到Linux,我想知道这是否会带来问题。 If it does use different line endings for Unix and Windows, what do you recommend? 如果在Unix和Windows中确实使用不同的行尾,那么您有什么建议?

You're fine, ConfigParser will still work. 很好, ConfigParser仍然可以工作。

The reason is that is uses fp.readline , which reads up to and including the next LF ( \\n ). 原因是使用fp.readline ,它最多读取并包括下一个LF( \\n )。 The value is then stripped of whitespace, which removes the CR ( \\r ). 然后,该值将被去除空白,从而删除了CR( \\r )。

I'd say just use LF ( \\n ) as your line separator - it will work on both systems, but using both won't cause any harm either. 我想说的只是使用LF( \\n )作为您的行分隔符-它在两个系统上都可以使用,但是同时使用这两个都不会造成任何伤害。

Edit: In fact, if you generate a file using ConfigParser.RawConfigParser it will use \\n as the line separator. 编辑:实际上,如果使用ConfigParser.RawConfigParser生成文件,它将使用\\n作为行分隔符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM