5

I'm working with Super CSV and it looks like an amazing package.

My only worry is how to work with columns with spaces in their names. No, I cannot go back and remove the spaces myself. These files will be given to me by the hundreds and I don't have time to go back and fix all 60 columns for each file and I can't trust everyone else to do it properly.

How can I work with columns with spaces in the title (i.e. "First Name" not "FirstName" or "firstName")?

Thanks!

For coding samples, look here: http://supercsv.sourceforge.net/codeExamples_general.html

James Bassett
  • 9,458
  • 4
  • 35
  • 68
Justian Meyer
  • 3,623
  • 9
  • 35
  • 53
  • CSV is comma-separated-values, so there should be no issue with spaces in field names. Why do you suggest that these should be "fixed" if you had the time? Could it be that the field names are "supposed" to have spaces, and if you removed them, it would be an error? – maxwellb Jun 29 '10 at 13:13
  • I'm confused. What? I'm allowed to alter the data as I please and I know it would work without spaces. What do you mean "supposed" to have spaces. The csv files I'm using weren't developed for this automated process, their just on file. – Justian Meyer Jun 29 '10 at 13:17
  • I do apologize. When you said "how am I supposed to work with spaces in the title", I thought perhaps you couldn't parse the field names properly. If you're not reading the header, everything is positional from then out, and you can use the headers, if you read them, as hints to what the position in the CSV rows mean, translating that to your application specific needs. – maxwellb Jun 29 '10 at 13:24
  • FYI [Super CSV 2.0.0-beta-1](http://supercsv.sourceforge.net/release_notes.html) is out now. It includes many bug fixes and new features (including Maven support and a new Dozer extension for mapping nested properties and arrays/Collections). – James Bassett Sep 18 '12 at 06:24
  • Some Super CSV site pages are offline, such as http://supercsv.sourceforge.net/codeExamples_general.html – dev_nut Oct 23 '12 at 20:06

4 Answers4

5

You notice this line in the samples you link to:

final String[] header = inFile.getCSVHeader(true);

This should give you your column names, no?

http://supercsv.sourceforge.net/javadoc/index.html

I think I understand your question now. The String[] argument passed to the read function takes the property names of the class you want to read into. It is positional, so it doesn't have to be named anything like the headers. So, for example, you can have String[] header = inFile.getCSVHeader(), but then have a mapping of headerName->propertyName, so, if your header fields were:

First Name, Last Name, Age

but your class was

getFirstName(), setFirstName(...);
getLastName(), setLastName(...);
getYears(), setYears();

pass to the read method not (String[]) {"First Name", "Last Name", "Age"}, as your header is, but pass to read, a (String[]) {"FirstName", "LastName", "Years"} array.

maxwellb
  • 13,366
  • 2
  • 25
  • 35
  • Look at the UserBean class, though. Doesn't the column name have to correspond to the variables? I'm a little confused what happens at the while loop. – Justian Meyer Jun 29 '10 at 13:15
  • What I'm saying is, notice how he uses the headers "username", "password", etc. These directly correspond to the variables username and password. I have "First Name" as a header and can't have First Name as a variable (spaces aren't allowed + poor formatting). – Justian Meyer Jun 29 '10 at 13:19
  • I was thinking about that, but if I define my own header, wouldn't it consider my headers that are already in place as data? I'll try it out and get back to you. Might take a sec with so many variables. – Justian Meyer Jun 29 '10 at 13:24
  • 1
    No, according to the JavaDoc, everything in the `read` methods only correspond the position of the String[] to the position of the field. This is easily seen if you use a CsvListReader instead of a CsvBeanReader, as there are no name mappings of field names to speak of. You only care about position with CsvListReader. With CsvBeanReader, String[] {FName,LName} says "read the first field into the FName property, and the second field into the LName property". After the header, Csv*Reader doesn't care what you name the fields. It's 1st field, 2nd field, 3rd field, etc. – maxwellb Jun 29 '10 at 13:31
  • It all works perfectly now. Thanks :) EDIT: It does include the headers like I expected it to, but those can easily be worked around. – Justian Meyer Jun 30 '10 at 12:25
3

To have a header with spaces separate the bean mapping and header array.

private final String[] header = new String[] { "First Name", "Last Name"}; private final String[] dataMapping = new String[] { "firstName", "lastName"}; beanWriter = new CsvBeanWriter(new FileWriter(path), CsvPreference.EXCEL_PREFERENCE); beanWriter.writeHeader(header); beanWriter.write(yourPojo, dataMapping, processors);

chroyet
  • 31
  • 1
2

If you want to map CSV header with different name, you can create a hash map and use this for your internal implementation, e.g. you can map "First Name" with "firstName" and populate your bean based on your internal names..

Rakesh Goyal
  • 3,117
  • 8
  • 39
  • 69
1

I realize this is quite an old post but others may be looking for an alternative simple solution. This is what I did to ensure I had only alph numeric header names with no spaces:

  final String[] header = beanReader.getHeader(true);

  for(int i = 0; i <= header.length -1 ; i++ ) {
    header[i] = header[i].replaceAll("[^a-zA-Z0-9]+","");
  }