Skip to content Skip to sidebar Skip to footer

One Extra Line Getting Added When I Open Csv File On Window. This Is Observed Only When I Send Csv File As Attachment To Email

I have python script that generates CSV file by reading multiple csv files as input. from linux machine i am sending that csv file as an email attachment using mail command. when i

Solution 1:

You might be running into trouble with different platform specific line endings.

Windows uses \r\n, Macs (before OS X) used \r, UNIX uses \n.

I suggest to inspect the .csv that causes problems with a hex editor or hexdump:

# hexdump -C test.csv
00000000  31 3b 32 3b 33 0a 61 3b  62 3b 63 0a              |1;2;3.a;b;c.|
0000000c

(The 0a in here is a newline (\n). Windows' \r\n would be 0d 0a).

In order to (testwise) convert files from UNIX to Windows line endings (and vice-versa), you can use the dos2unix / unix2dos tools.

Solution 2:

There are discussions related to the CSV writer behavior with respect to the occurrence of additional new lines. Also, the behavior seems to be different from 2.7 and 3.0 versions.

This link talks about the occurrence of such an issue. However this SO question gives a solution.

As per y understanding it is always good to take control of how newline has to be written by the CSV writer by doing something like

csv.writer(sys.stdout, lineterminator='\n')

Post a Comment for "One Extra Line Getting Added When I Open Csv File On Window. This Is Observed Only When I Send Csv File As Attachment To Email"