Menu

#163 Allow header transformation in CsvReaderHeaderAware

open
nobody
None
5
2023-12-07
2023-12-06
Kevin S
No

I have a use case where the files I'm parsing have a couple of problems.

  1. There is a trailing comma on the end of every row, including the header
  2. There are some unprintable characters in some of the header columns.

I'd like to use CsvReaderHeaderAware, but as far as I can tell there isn't any way that I can transform the header row to fix the issues above.

Ideally, I'd like to be able to specify a RowProcessor for the header separately from the RowProcessor for the data rows. That would allow me to fix the unprintable character issue.

I'm not sure how to solve the first problem though. Is there a way to ignore a trailing comma in both the header and data rows?

Discussion

  • Scott Conway

    Scott Conway - 2023-12-06

    So on the first problem are you seeing a comma at the end of every row or the end of every line? That is important for two reasons - the first is that csv files can have data that has multiple new line characters. And second opencsv uses the java Reader class which reads the file one line at a time to create a row. If your data has no newlines in it so each line in the file is exactly one row of data or you have a comma at the end of each line then you are good. In this case I would recommend you use linux to remove the character for you - I would use the sed or awk solution found in https://www.baeldung.com/linux/remove-last-character-from-line

    For removing non printable characters you can use the *nix tr command - https://alvinalexander.com/blog/post/linux-unix/how-remove-non-printable-ascii-characters-file-unix/

     
  • Kevin S

    Kevin S - 2023-12-07

    Hi Scott,
    Thanks very much for responding and thanks for the suggestions.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.