escape empty lines and comment lines
Brought to you by:
aruckerjones,
sconway
Hi,
I'm writing a paper about how/why/when to read CSV files, for my studdents. I'd like to end this paper with OpenCsv.
The preview is here : http://thierry-leriche-dessirier.developpez.com/tutoriels/java/csv-avec-java/#LXII
But there are 2-3 things I don't find in OpenCsv (you can see on the paper that I tried to deal them by myself) :
* How to escape empty lines ?
* How to escape comment lines (ie. lines that start with #) ?
* How to map lines ? as the first line contains the titles then we can map the other lines in a map<title, value>
Thx
The last one the only one with the easy answer: In CSVReader you can set the number of lines to skip before processing (this allows you to have comments, titles, etc at the start of the file).
7
108 /**
109 * Constructs CSVReader with supplied separator and quote char.
110 *
111 * @param reader the reader to an underlying CSV source.
112 * @param separator the delimiter to use for separating entries
113 * @param quotechar the character to use for quoted elements
114 * @param line the line number to skip for start reading
115 */
Knowing that you can then read the first line into your title array and the rest into your data.
Aside from that once the data starts openCSV does not make any assumptions about blank lines or comments. If you are using CSVReader or CSVParser you have to check the first character yourself to see if its a "comment" character.
For blank lines you just need to check the array that is returned. An blank line will return an array of size 1 with an empty string as the only element. Which is what your code is doing. Conversely since you know how many items you are reading in (from the header line) if you read/parse an array that is of different size you can ignore it instead of throwing an exception (I would throw exception if the array size was greater than one though because that to me is the sign of malformed data).
Looking at your code you might want to consider using a LinkedHashMultimap from the guava libraries instead of building a the map yourself - http://guava-libraries.googlecode.com/svn/trunk/javadoc/com/google/common/collect/LinkedHashMultimap.html.
Good luck with your paper. Let me know if I did not answer the questions or if you have more questions.
:)