简体   繁体   中英

python fileinput line endings

I've got some python code which is getting line endings all wrong:

command = 'svn cat -r {} "{}{}"'.format(svn_revision, svn_repo, svn_filename)
content = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE).stdout.read()

written = False
for line in fileinput.input(out_filename, inplace=1):
        if line.startswith("INPUT_TAG") and not written:
            print content
            written = True
        print line,

This fetches a copy of the file called svn_filename, and inserts the content into another file called out_filename at the "INPUT_TAG" location in the file.

The problem is the line endings in out_filename. They're meant to be \\r\\n but the block I insert is \\r\\r\\n.

Changing the print statement to:

print content,  # just removes the newlines after the content block

or

print content.replace('\r\r','\r')  # no change

has no effect. The extra carriage returns are inserted after the content leaves my code. It seems like something is deciding that because I'm on windows it should convert all \\n to \\r\\n.

How can I get around this?

CRLF = Carriage Return Line Feed.

Python on Windows makes a distinction between text and binary files; the end-of-line characters in text files are automatically altered slightly when data is read or written.

https://docs.python.org/2/tutorial/inputoutput.html#reading-and-writing-files

Can you output as binary file and not as a text file?

If you prefix the string with r to open the file as raw , does this prevent extra \\r in the output?

I can "solve" this problem by doing the following:

content = content.replace('\r\n', '\n')

converting the newlines to unix style so when the internal magic converts it again it ends up correct.

This can't be the right/best/pythonic way though....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM