Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meaning of Empty Line in CSV File

Tags:

csv

At first this seemed obvious, but now I'm not so sure.

If a CSV file has the following line:

a,

I would interpret that as two fields with the values "a" and "". But then looking at an empty line, I could just as easily argue that it signifies one field with the value "".

I accept that an empty line at the end of the file should be interpreted as the end of the file (no field). But does anyone have any information on what an empty line within the file should mean?

like image 926
Jonathan Wood Avatar asked Oct 05 '12 16:10

Jonathan Wood


1 Answers

Looking at how Excel handles empty lines when reading CSV files, I can see that Excel does not ignore them.

Unfortunately, there is no way to tell if the empty line was treated as an empty field or no fields at all because Excel always has the same number of columns.

I saw some proprietary uses of the CSV format where there was an option to how blank lines should be treated. In the end, this is the approach I took. My CSV reader class has four options for how to deal with empty lines:

  1. Ignore and skip over them
  2. Treat them as a row with zero fields
  3. Treat them as a row with one empty field
  4. Treat them as the end of the input file

If anyone's interested, I will be posting the new source code to replace the existing article at Reading and Writing CSV Files in C#.

like image 129
Jonathan Wood Avatar answered Oct 06 '22 13:10

Jonathan Wood