How to convert CRLF to LF on a Windows machine in Python

So, I got this template, they all end in LF, and I can fill in some terms inside with the format and still get LF files, opening with "wb"

These patterns are used in deploying a script on a Windows machine to deploy to a unix server.

The problem is that many people are going to mess with this template, and I'm 100% sure that some of them will put some CRLFs inside.

How could I, using python, convert all crlf to lf?

Thanks.

EDIT

Well, I'm bad, I had an error in my code, opening in "wb" always put lf at the end of lines, even if the file used crlf before.

Here is the code I use if you're interested:

#!/usr/bin/env python # --*-- encoding: iso-8859-1 --*-- import string def formatFile(templatePath, filledFilePath, params, target): openingMode = 'w' if target == 'linux': openingMode += 'b' with open(templatePath, 'r') as infile, open(filledFilePath, openingMode) as outfile: for line in infile: template = string.Template(line.decode('UTF-8')) outfile.write(template.substitute(**params).encode('UTF-8')) 

So there are no problems, everything works fine: x

+6
source share
4 answers

The 'rU' Python function supports the 'rU' mode for generic newlines , in which case it does not mind what type of newlines each line has. In Python 3, you can also request a specific newline form with the newline argument to open .

Thus, the transition from one form to another is quite simple in Python:

 with open('filename.in', 'rU') as infile, \ open('filename.out', 'w', newline='\n') as outfile: outfile.writelines(infile.readlines()) 

(Due to the newline argument, U is not actually recommended in Python 3, the equivalent form is newline=None .)

+6
source

Convert line endings in place (using Python 3)

Windows for Linux / Unix

The following is a short script for directly converting Windows line endings ( \r\n also called CRLF ) to the end of a Linux / Unix line ( \n also called LF ) in place (without creating an additional output file):

 # replacement strings WINDOWS_LINE_ENDING = b'\r\n' UNIX_LINE_ENDING = b'\n' # relative or absolute file path, eg: file_path = r"c:\Users\Username\Desktop\file.txt" with open(file_path, 'rb') as open_file: content = open_file.read() content = content.replace(WINDOWS_LINE_ENDING, UNIX_LINE_ENDING) with open(file_path, 'wb') as open_file: open_file.write(content) 

Linux / Unix for Windows

Just change the line ending to content.replace(UNIX_LINE_ENDING, WINDOWS_LINE_ENDING) .


Code designation

  • Important: binary mode. We need to make sure that we open the file both times in binary mode ( mode='rb' and mode='wb' ) to convert to work.

    When opening files in text mode ( mode='r' or mode='w' without b ) the end of the platformโ€™s own lines ( \r\n on Windows and \r on older versions of Mac OS) is automatically converted to Python Unix - end of line : \n . Therefore, the call to content.replace() could not find any line endings to replace.

    In binary mode, such a conversion is not performed.

  • Binary strings In Python 3, unless otherwise specified, strings are stored as Unicode ( UTF-8 ). But we open our files in binary mode - so we need to add b in front of our replacement lines to tell Python to also treat these lines as binary.

  • Raw Strings On Windows, the path separator is the backslash \ that we will need to escape in a regular Python string with \\ . By adding r before the line, we create the so-called raw line, which does not need to be escaped. This way you can directly copy / paste the path from Windows Explorer.

  • Alternative We open the file twice to avoid the need to rearrange the file pointer. We could also open the file once with mode='rb+' but then we would need to move the pointer back to start reading it ( open_file.seek(0) ), and open_file.seek(0) its original contents before writing new one ( open_file.truncate(0) ).

    Just opening a file in recording mode does this automatically for us.

Cheers and happy programming,
winklerrr

+4
source

You can fix existing bad ending templates with this code:

 with open('file.tpl') as template: lines = [line.replace('\r\n', '\n') for line in template] with open('file.tpl', 'w') as template: template.writelines(lines) 
0
source

why don't you try below: str.replace ('\ r \ n', '\ n');

CRLF => \ r \ n LF => \ n

this is the story of a typewriter =)

0
source

All Articles