2

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.

Ram More
  • 81
  • 1
  • 10
  • can u paste the code about how the CSV files are being read? – Prahalad Deshpande Oct 27 '13 at 14:04
  • @PrahaladDeshpande I don't think he reads the CSV with Python - they are supposed to be opened in MS Excel on a Windows platform. – Lukas Graf Oct 27 '13 at 14:05
  • Why negative voting i have tried all the ways like python open option w, wb, w+r ... i have added lineterminator=\n while opening file for writing. inserted sep=, as separator in csv file. nothing is working thats why i posted this question. – Ram More Oct 27 '13 at 14:05
  • @LukasGraf am reading CSV input files with python only. i will post whole script wait. – Ram More Oct 27 '13 at 14:07
  • Python 3 or Python 2? – Steven Rumbalski Oct 27 '13 at 14:10
  • @StevenRumbalski i am using python 2.6 i have posted whole script which reads input csv and generates result.csv – Ram More Oct 27 '13 at 14:12
  • I don't see a problem with the Python code. So where you save the mail attachment on the Windows box, and open that with notepad or a hex editor, what sort of line endings does it have? – Lukas Graf Oct 27 '13 at 14:28

2 Answers2

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.

Lukas Graf
  • 30,317
  • 8
  • 77
  • 92
  • i tried to print hexdump i can see 0a most of the times. so in my case \n is newline character i guess. if yes then it should work on windows. – Ram More Oct 27 '13 at 14:36
1

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')
Community
  • 1
  • 1
Prahalad Deshpande
  • 4,709
  • 1
  • 20
  • 22