Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Remove unwanted double quote from .csv file

 
Sunil Nagotra
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello

I have a .csv file like this:

Field1,Field2
My Name is,"My ""Pet"" Name is"

When a split form (,) the string i get following in text file
Field1,Field2
"My Name is", """My """"Pet"""" Doe"""

I want to get output same as the input.
Any buddy please help me.

Thanks in Advance.
Sunil
 
D. Ogranos
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String.split() does not add quotes as far as I know. So it looks like you got the extra quotes somewhere else. Please post exactly what you're doing (and use code tags ;)
 
Sunil Nagotra
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Thanks for reply

Hi

I have .csv file which have some record with double code.

Like

My Name is,"My ""Pet"" Name is"

I want to write its contents into a text file
the contents of generated file show extra double code.

output files show

"My Name is","""My """"Pet"""" Name is"""

I am using following code.


Excepted output will be

"My Name is",""My ""Pet"" Name is""

Thanks,
Sunil


>
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're better off using a CSV library (such as the ones mentioned in the AccessingFileFormats page) that know when to add and when to remove the double quotes.
 
Campbell Ritchie
Sheriff
Posts: 48424
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Please always use the code button. Since you are new, I have edited your post and you can see how much better it looks. Also always use spaces for indentation, not tabs.
 
D. Ogranos
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Something's still missing in there. You're reading an entire line with the BufferedReader.readLine(). This method does not add extra quotes. Print out the input line after reading it, it should look exactly like in the .csv file.

Then you split the line. That method also doesn't add quotes. Print out each token, you should get two tokens:
1) My Name is
2) "My ""Pet"" Name is"

Then you write the tokens. FileWriter also doesn't add quotes. Note that you are writing a comma after each token, that should appear in your output as well (also after the last token!)...it doesn't, so either you're using another method than you think you do, or something else is amiss here.
 
Sunil Nagotra
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I still Have same problem
I have attached csv file
following code used to display the output but still getting same output.
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not following; are you saying that this piece of code produces different output than you get if you open the file in a text editor (NOT a spreadsheet application)?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic