Python line break question: RN or n?

Source: Internet
Author: User
Execute the following Python code:

fn = ' Test.txt '
File (FN, ' w+ '). Write (' Test\ntest2 ')
Content = File (FN, ' R '). Read ()
Print Content.replace (' \ R ', ' \\r '). replace (' \ n ', ' \\n ')

The printed results are not platform dependent, and are:
Test\ntest2
But the content of Test.txt depends on the platform and changes (with notepad++, VI and other programs to view):
Under Windows is: Test\r\ntest2
Linux is: Test\ntest2

The question I want to ask is, how do you ensure that the ' \ n ' character of Python writing to a file does not change with the platform, which is guaranteed to be ' \ n '?

Reply content:

Zhang Yi, JS & python
Summer one by one, Joe 3 less agree
This is not a Python problem, Windows line break is \r\n,unix is the \n,mac is \ r.
This is a very classic question. Because the default line break characters are different under different systems. When dealing with characters, such "differences" can cause great problems, such as line[-2] and Line.strpi (), which return different values because of different platforms.

Workaround:
Python 2
(PEP 278--Universal Newline support , thanks to Bi Qin's supplement ):
1) If it is not txt file, it is recommended to use WB and RB to read and write. There is no line-wrapping problem with binary reading and writing.
2) If you need clear text, use RU to read ( Highly Recommended), i.e. u Universal Wrap mode (Universal new line mode). This mode replaces all newline characters (\ r \ n \ r \ n) with \ n. Read-only support, but enough. This is one of the best options that Python has to offer to us.

Compare the results of R and RU:
content = file(fn, 'r').read()# test\r\ntest2# 这里的换行会因不同系统而不同                       
file (FN, ' w+ '). Write (' Test\ntest2 ')
Change into
file (FN, ' wb+ '). Write (' Test\ntest2 ')
On the line. Python distinguishes Binary and ASCII patterns on the Windows platform. The line break in ASCII mode automatically changes to \ r \ n When reading and writing (see [1]). There is no such difference under the *nix platform.
[1]: / http Docs.python.org/tutoria L/inputoutput.html#reading-and-writing-files Python can automatically select line breaks according to the system, as long as the use of os.linesep can be determined that this is not a Python problem, Windows line break is \r\n,unix is \n,mac is \ r. The answer was slightly tricky except for the first one. As a personal rule, when editing a file, use the binary format as much as possible, which avoids errors across platforms. There is no need to know if each platform is \ r, or \ n. Just get into the habit of RB,WB and so on. Rio is a good programming habit. If not specifically specified, Python will adapt to different platform differences, such as \ n and \ r \ n.
Want to reach the landlord's eyes, you can use the binary mode to open and manipulate the file. Now this answer has been wrong 0.0, correct as follows (in the foreseeable future, the answer is probably wrong, expecting someone @ me).
In ancient times: the line of Windows is \r\n,unix is the \n,mac is \ r is absolutely correct!
The line break for Mac OS 9 and the previous system is CR (\ r), and the newline character starting with Mac OS X (later renamed "OS X") is LF (\ n).
Reference did MAC OS Lion switch to using line feeds (LF ' \ n ') for line breaks instead of carriage returns (CR ' \ R ')? One thing I know is that a file ending with CLRF can be converted to a file with the end of the RF with the "Dos2unix" command.
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.