-1

I am trying to parse a CSV file using Super CSV containing two fields (tab separated) with a unquoted string.

    occugroup_code  occugroup_name
    110000          Management Occupations  
    130000          Business and Financial Operations Occupations   
    150000          Computer and Mathematical Occupations   

I have trouble to figure out how to configure the CsvPreference to be able to return a Map for each two. Anyone ran into this problem ?

James Bassett
  • 9,458
  • 4
  • 35
  • 68
fellahst
  • 641
  • 1
  • 7
  • 16
  • You already tried it in any way, get an exception or anything like that? – timaschew Aug 07 '11 at 07:09
  • 2
    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). And a tab-delimited preference `CsvPreference.TAB_PREFERENCE` :) – James Bassett Sep 18 '12 at 06:26

1 Answers1

8

Please try to do anything on your own next time or describe your problem more concrete.
For example:
I try the CsvPreference.xyz but it didn't work, because I get the exception abc


Some basic stuff: CSV = Comma-separated values
You file isn't seperated with comma or semicolon, its seperated with tabs. So you have to create your own CsvPreference:
CsvPreference pref = new CsvPreference('\"', '\t', "\n");

Here is the full example (tested):

InputStream inputStream = this.getClassLoader().getResourceAsStream("example.csv");
CsvPreference pref = new CsvPreference('\"', '\t', "\n");
ICsvMapReader reader = new CsvMapReader(new InputStreamReader(inputStream), pref);

List<Map<String, String>> list = new ArrayList<Map<String, String>>();
Map<String, String> result;
while ((result = reader.read(new String[]{"code", "name"})) != null) {
    list.add(result);
}

for (Map<String, String> elem : list) {
    System.out.print(elem.get("code")+" | ");
    System.out.print(elem.get("name"));
    System.out.println();
}

Output:

110000 | Management Occupations
130000 | Business and Financial Operations Occupations
150000 | Computer and Mathematical Occupations

timaschew
  • 16,254
  • 6
  • 61
  • 78