Python line breaks: rn or n?

Source: Internet
Author: User
Run the following Python code: fntest.txt file (fn, w + ). write (testntest2) contentfile (fn, r ). read () printcontent. replace (r, \ r ). the output of replace (n, \ n) does not depend on the platform. All of them are testntest2but the content of test.txt changes depending on the platform (Use note to 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 do not depend on the platform:
Test \ ntest2
However, the content of test.txt depends on the platform (which can be viewed using notepad ++, vi, and other programs ):
In windows: test \ r \ ntest2
In linux, it is: test \ ntest2

The question I want to ask is, how to ensure that the '\ n' character written to the file by Python does not change with the platform, that is, it must be' \ n '? Reply content:
Zhang Shuo, js & python
Xia Yi and Qiao Shao agree
This is not a python issue. The windows line feed is \ r \ n, unix is \ n, and mac is \ r.
This is a classic problem. Because the default line breaks vary in different systems. During character processing, such a "different" will cause a big problem. For example, line [-2] And line. strpi () will return different values for different platforms.

Solution:
Python 2
(PEP 278 -- Universal Newline Support Thank you for adding ):
1) if it is not a txt file, we recommend using wb and rb to read and write data. Read and Write Data in binary format without line breaks.
2) If you need plain text, use rU to read ( Highly recommended), That is U Universal line feed mode (Universal new line mode ). This mode replaces all linefeeds (\ r \ n) with \ n.Read only, but it is enough. This is one of the best options provided by Python.

Comparison of r and rU results:

Content = file (fn, 'R'). read () # test \ r \ ntest2 # The line feed here varies with different systems
File (fn, 'W + '). write ('test \ ntest2 ')
Change
File (fn, 'wb + '). write ('test \ ntest2 ')
That's all. On Windows, Python distinguishes Binary from ASCII. In ASCII mode, the line break is automatically changed to \ r \ n during reading and writing (For details, refer to [1]). * There is no such difference on the nix platform.
[1]: http://docs.python.org/tutorial/inputoutput.html#reading-and-writing-files Python can automatically select line breaks based on the system, as long as the OS is used. linesep can tell that this is not a python issue. In windows, the line feed is \ r \ n, unix is \ n, and mac is \ r. The answer is slightly incorrect except for the first one. personally, when editing a file, try to use the binary format as much as possible, which will avoid errors across platforms. you do not need to know whether each platform is \ r or \ n. you just need to develop rb, wb and other habits. rio is a good programming habit. if this parameter is not specified, python will adapt to the differences between different platforms, such as the differences between \ n and \ r \ n.
To achieve the goal of the landlord, you can open and operate files in binary mode. Now the answer is 0.0 wrong. The answer is correct as follows (in the foreseeable future, this answer may also be wrong. Someone is expected to ask me ).
In ancient times, the windows line feed was \ r \ n, unix was \ n, and mac was \ r completely correct!
The linefeed of Mac OS 9 and earlier systems is CR (\ r), and The linefeed starting from Mac OS x (later renamed as "OS X") is LF (\ n ).
Refer to 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 files ending with CLRF can be converted to files ending with RF using 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.