one extra line getting added when i open CSV file

2019-07-11 03:12发布

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 open that attached file on window's machine using MS Excel its adding one extra blank line line between each line present in csv. when i open that file in linux using vi editor its fine only four lines are present in csv file. Its more strange when i copy that file using winSCP from linux to window and open in MS Excel file is good no extra blank lines. Is there anything wrong in mail command or my script is not generating correct csv file.

can anybody find something wrong that i am missing in this process.

2条回答
对你真心纯属浪费
2楼-- · 2019-07-11 03:22

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')
查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-07-11 03:27

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.

查看更多
登录 后发表回答