Python adds extra CR at the end of the resulting lines

A Java application sends XML to a Python application. They are both on the same machine. When I open the resulting file, I see additional lines (due to additional CR). 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 source file:

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

It is received:

 <root><CR> <CR><LF> <SONG><CR> <CR><LF> <ARTIST>Coldplay</ARTIST><CR> <CR><LF> </SONG><CR> <CR><LF> </root><CR> <CR><LF> 
+7
source share
1 answer

Change filemode from 'w' to 'wb' , otherwise Python will convert any newlines ( '\n' ) to a specific platform view ( '\r\n' for Windows). Binary mode suppresses this conversion.

+7
source

All Articles