Python converts carriage return \ r \ n to \ n, python

Source: Internet
Author: User

Python converts carriage return \ r \ n to \ n, python

Recently in the concise configuration of cocos2d-x, found some friends of the text editor, automatically cut \ r \ n into \ n, (on unix line feed using \ n, windows, the newline uses \ r \ n). Therefore, I wrote this script to help some friends. I don't need to change it in one line.

Import osdef replace (filePath, w2u): try: oldfile = open (filePath, "rb +") # use B to open path, name = OS. path. split (filePath) newfile = open (path + '$' + name, "ba +") old = B 'new = B' if w2u = True: old = B '\ R' new = B' else: old = B '\ n' new = B' \ r \ n' data = B 'while (True ): data = oldfile. read (200) newData = data. replace (old, new) newfile. write (newData) if len (data) <200: break newfile. close () oldfile. close () OS. remove (filePath) OS. rename (path + '$' + name, filePath) failed t IOError as e: print (e) if _ name _ = "_ main __": print ("Enter the file path:") filePath = input () replace (filePath, False) # If this parameter is set to True, \ n can be changed to \ r \ n.

Note that in python, symbols such as \ r \ n cannot be found if the text is opened, only '\ n' can be found, so it must be enabled in B (Binary) mode.

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.