Python Adds An Extra CR At The End Of The Received Lines
Asked Answered
H

1

7

A Java application sends an XML to a Python application. They are both on the same machine. When I open the received file I can see extra lines (because of extra CRs). What could be the reason for this?

This is the receiver:

f = open('c:/python/python.xml', 'w')
while 1:
    print("xxx")        
    data = socket.recv(recv_frame)
    remain_byte = remain_byte - len(data)
    print(remain_byte)
    f.write(data)
    if (something):
        break

This is the sender:

    while ((bytesRead = file_inp.read(buffer)) > 0) {
        output_local.write(buffer, 0, bytesRead);
    }

This is the original file:

<root><CR><LF>
    <SONG><CR><LF>
        <ARTIST>Coldplay</ARTIST><CR><LF>
    </SONG><CR><LF>
</root><CR><LF>

This is the received:

<root><CR>
<CR><LF>
    <SONG><CR>
<CR><LF>
        <ARTIST>Coldplay</ARTIST><CR>
<CR><LF>
    </SONG><CR>
<CR><LF>
</root><CR>
<CR><LF>
Halfwitted answered 11/4, 2012 at 17:10 Comment(6)
What operating system are the applications running on? Is it windows?Velez
Are you looking at the file "c:/python/python.xml"? On windows, some of the C libraries do carriage-return/line-feed processing to make UNIX files compatible with windows files. Unix only uses one character for line separation (\n), but Windows uses cr+lf. So it is just-about possible that two sets of processing (Java and Python) are expanding one line separator to two,Velez
Yes, the file I'm checking is correct.Halfwitted
Change filemode from 'w' to 'wb'.Grant
IIRC use "wb" when opening files for write, and that suppresses Python doing line separator expansion.Velez
wb solved the issue! great, thanks!Halfwitted
G
7

Change filemode from 'w' to 'wb', otherwise Python converts any newlines ('\n') into the platform specific representation ('\r\n' for Windows). Binary mode suppresses this conversion.

Grant answered 11/4, 2012 at 17:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.