Created
August 25, 2010 16:56
-
-
Save seanh/549851 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| text = open('unknownEndings.txt','rU').read() | |
| open('localEndings.txt','w').write(content) | |
| lines = open('unknownEndings.txt','rU').readlines() | |
| open('localEndings.txt','w').writelines(lines) |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To convert a file with any type of newlines to the local system's newlines read the file in
rUmode then write it inwmode.Different operating systems use different escape sequences for newlines (e.g.
\n,\r,\r\n...).Python uses
\n's for newlines internally, but when writing a file in text mode (wnotwb) on Windows it converts\n's to Windows newlines. If the text that you pass towrite()orwritelines()contains Windows newlines (or some newlines other than\n's), maybe because you read that text from a file that contains those newlines, these will not be converted to the local system's newlines when you write to file.If you read a file in universal mode (
rUinstead ofr) then Python will convert all sorts of newlines to\n's. Therefore, you can convert a file to the local system's newlines format by reading the file inrUmode and then writing it inwmode.Also see: http://docs.python.org/release/2.6.5/library/functions.html#open.