• Post Reply Bookmark Topic Watch Topic
  • New Topic

Issue reading csv lines that contain "  RSS feed

 
Tiberius Marius
Ranch Hand
Posts: 115
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I need to read a csv file from the disk and manipulate the data inside.My problem is that if the csv (excel format) contains "" in any of it's values the whole string line gets saved/displayed incorrectly . For example for the line in the csv file :

1,393275,57319 57321 57323 57325 57327,5_5,200000,393277,57329 57331 57333 57335 57337,3_3,200000,400000,"gi,gi",,,,,,BASS/2.4,,1

it gets read in java as :

"1,393275,57319 57321 57323 57325 57327,5_5,200000,393277,57329 57331 57333 57335 57337,3_3,200000,400000,""gi,gi"",,,,,,BASS/2.4,,1";;;;

instead as :

1,393275,57319 57321 57323 57325 57327,5_5,200000,393277,57329 57331 57333 57335 57337,3_3,200000,400000,"gi,gi",,,,,,BASS/2.4,,1

My code is :



What am i doing wrong , how can i read a csv file (excel saved) without getting "" added incorrectly ?
 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Parsing CSV supporting the full specification (including ", escaping " when it appears in a value etc.) is not trivial. My advice would be to not do it yourself and use a CSV parsing library. For example, http://commons.apache.org/proper/commons-csv/

Edit: I may have misread the specific problem you are having - sorry - although I'd still stand by the advice. It seems unlikely, though, that Java is reading characters that aren't there.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tiberius Marius wrote:What am i doing wrong,

Nothing, as far as I can see. And that's the problem: you're not doing anything (at least not in the code you posted). You're simply reading a line and printing it out.
how can i read a csv file (excel saved) without getting "" added incorrectly ?

Follow Matthew's advice. You might be able to get something close with l.split(","), but it's unlikely to work in every case. As he says, CSV is not simple to implement correctly.

Winston
 
Tiberius Marius
Ranch Hand
Posts: 115
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem was the regional settings on my computer . Once set to US the section i posted outputs expected results , aka the exact line from the csv file.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tiberius Marius wrote:The problem was the regional settings on my computer . Once set to US the section i posted outputs expected results , aka the exact line from the csv file.

???

Based on the code you posted, I can't see how that would make a blind bit of difference - unless it changes what Excel produces - because all I see is a Java version of cat.

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!