aspose file tools*
The moose likes Java in General and the fly likes Remove unwanted double quote from .csv file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Remove unwanted double quote from .csv file" Watch "Remove unwanted double quote from .csv file" New topic
Author

Remove unwanted double quote from .csv file

Sunil Nagotra
Greenhorn

Joined: Apr 13, 2011
Posts: 6
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

Joined: Feb 02, 2009
Posts: 214
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

Joined: Apr 13, 2011
Posts: 6
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
Marshal

Joined: Mar 22, 2005
Posts: 42276
    
  64
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.


Ping & DNS - my free Android networking tools app
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39396
    
  28
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

Joined: Feb 02, 2009
Posts: 214
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

Joined: Apr 13, 2011
Posts: 6
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
Marshal

Joined: Mar 22, 2005
Posts: 42276
    
  64
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)?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Remove unwanted double quote from .csv file