Unwanted double quotes in generated csv file

▼魔方 西西 提交于 2019-11-27 03:03:48

问题


I have created a CSV file using the Java code below:

String csv = rs.getString("UPLOAD_FOLDER_PATH")+".csv";
CSVWriter writer = new CSVWriter(new FileWriter(csv));
String [] filevalues = new String[filevaluesarray.size()];

filevalues=filevaluesarray.toArray(filevalues);

writer.writeNext(filevalues);

writer.close();

I am getting the CSV file, but the content of the file has unwanted double quotes.

Eg. "ABC","123","KDNJ"

I don't get from where these double quotes are added.


回答1:


This worked for me

CSVWriter writer = 
    new CSVWriter(new FileWriter(csv), ',', CSVWriter.NO_QUOTE_CHARACTER);

See the CSVWriter javadoc




回答2:


You should probably clarify what you mean by 'unwanted' quotes.

  1. I don't want it to quote everything, only the fields that contain embedded commas, quotes and newlines (quoting everything is unnecessary and makes my files bigger), or

  2. I don't want anything quoted, and I understand that my CSV will be invalid if it contains embedded commas, quotes and newlines

If it's the first option, then opencsv doesn't support this - it either quotes everything or nothing. Take a look at Super CSV if you want an open source CSV library that only quotes when necessary (and can quote everything too, if required).

If it's the second option, then go with Sheldon's answer, but just be aware the your CSV will be invalid if it contains embedded commas, quotes and newlines.

For example, if I'm reading your CSV file, how am I meant to know that the following is actually just a single record with 2 fields?

P Sherman, 42 Wallaby Way,
Sydney, AUSTRALIA

Whereas if it was quoted properly that would be obvious, i.e.

P Sherman, "42 Wallaby Way,
Sydney, AUSTRALIA"

FYI, here's the rules relating to quotes from RFC4180 (the MIME type definition for CSV).

5 Each field may or may not be enclosed in double quotes (however some programs, such as Microsoft Excel, do not use double quotes at all). If fields are not enclosed with double quotes, then double quotes may not appear inside the fields. For example:

   "aaa","bbb","ccc" CRLF
   zzz,yyy,xxx

6 Fields containing line breaks (CRLF), double quotes, and commas should be enclosed in double-quotes. For example:

   "aaa","b CRLF
   bb","ccc" CRLF
   zzz,yyy,xxx

7 If double-quotes are used to enclose fields, then a double-quote appearing inside a field must be escaped by preceding it with another double quote. For example:

   "aaa","b""bb","ccc"



回答3:


If you don't want the quotes in the values of the generated CSV file, you have to create the CSVWriter object in this way:

CSVWriter writer = new CSVWriter(new FileWriter(filePath),
    CSVWriter.DEFAULT_SEPARATOR,
    CSVWriter.NO_QUOTE_CHARACTER,
    CSVWriter.DEFAULT_ESCAPE_CHARACTER,
    CSVWriter.RFC4180_LINE_END);

The key is CSVWriter.NO_QUOTE_CHARACTER. You can customize the values of the other constructor parameters.




回答4:


private void writeFile(String fileAbsolutePath , ListcsvLines) throws IOException{

    final char csvDelimeter = ',';
    CSVWriter csvWriter = new CSVWriter(new FileWriter(new File(fileAbsolutePath)),csvDelimeter,CSVWriter
                                                                                               .NO_QUOTE_CHARACTER);
    CSVParser parser = new CSVParser();

    for(String csvLine  : csvLines){
        String[] csvVals = parser.parseLine(csvLine);
        csvWriter.writeNext(csvVals);
    }
    csvWriter.flush();
}

Call : writeFile(fileAbsolutePath,csvLinesList);

Working example for Shamis's answer ,It works fine for me.




回答5:


I also face the same problem with open csv and for fix the issue I use escaped character.

for ex:

CSVReader csvReader = new CSVReader(new FileReader(fileName), seprator,escaped_character);

In here open csv default use double quotes as escape_character(according to my knowledge)

In my case I use separator as pipe sign (|)

A2|G A A|Thilina|9022V|1|2|3|4|"Rubasingha"|'Abc|MATARA"|'No'|2012|1668.88

In here "Rubasingha" open and close the double quotes and in 'No' also open and close the single quotes. these two work perfectly in default open csv

but when we use 'Abc - only open single quotes - this also work fine

but MATARA" OR "MATARA - in here we have one double quote - in my case this generate error while reading the CSV using open csv

for fix the issue I reference this page (http://cs.swan.ac.uk/~csbob/teaching/java/JavaDemoNetbeans/opencsv-2.3/doc/)

There are constructors that cater for supplying your own separator and quote characters. Say you're using a tab for your separator, you can do something like this:

CSVReader reader = new CSVReader(new FileReader("yourfile.csv"), '\t');

And if you single quoted your escaped characters rather than double quote them, you can use the three arg constructor:

CSVReader reader = new CSVReader(new FileReader("yourfile.csv"), '\t', '\'');

You may also skip the first few lines of the file if you know that the content doesn't start till later in the file. So, for example, you can skip the first two lines by doing:

CSVReader reader = new CSVReader(new FileReader("yourfile.csv"), '\t', '\'', 2);

so I change the my default escape character to (^) sign without using double quotes as escape character.

CSVReader csvReader = new CSVReader(new FileReader(fileName), '|','^');

This is the way I fix the problem. Thanks




回答6:


I had a situation whereby I was facing data being displayed in my .csv file with three quotes on either side. This was due to my data having quotes in excel. The second I created a .csv file, I would open to see more quotations that was required. After much searching on the net I have found some code and tweaked to suit me as follows: -

 Public Sub OutputQuotedCSV()
 Const QSTR As String = ""
 Dim myRecord As Range
 Dim myField As Range
 Dim nFileNum As Long
 Dim sOut As String

   nFileNum = FreeFile
   Open "TheNameOfYourFile.txt" For Output As #nFileNum
   For Each myRecord In Range("A1:A" & _
          Range("A" & Rows.Count).End(xlUp).Row)
      With myRecord
         For Each myField In Range(.Cells(1), _
             Cells(.Row, 256).End(xlToLeft))
           'I didn't want my Header Row touched but wanted it added into the csv file
           'There's probably an easier way but this worked perfectly for me
            If myField.Text = "HEADER 1" Then 
                        sOut = sOut & QSTR & _
                        Replace(myField.Text, QSTR, QSTR & QSTR) & "  "
                    ElseIf myField.Text = "HEADER 2" Then
                        sOut = sOut & QSTR & _
                        Replace(myField.Text, QSTR, QSTR & QSTR) & "  "
                    ElseIf myField.Text = "HEADER 3" Then
                        sOut = sOut & QSTR & _
                        Replace(myField.Text, QSTR, QSTR & QSTR) & "  "
                    ElseIf myField.Text = "HEADER 4" Then
                        sOut = sOut & QSTR & _
                        Replace(myField.Text, QSTR, QSTR & QSTR) & "  "
                    ElseIf myField.Text = "HEADER 5" Then
                        sOut = sOut & QSTR & _
                        Replace(myField.Text, QSTR, QSTR & QSTR) & "  "
                    ElseIf myField.Text = "HEADER 6" Then
                        sOut = sOut & QSTR & _
                        Replace(myField.Text, QSTR, QSTR & QSTR) & "  "
                    Else
               'I didn't want my first column to start with "," so I added the code below
                        If myField.Cells.Column = 1 Then
                            sOut = sOut & QSTR & _
                            Replace(myField.Text, QSTR, QSTR & QSTR) & QSTR
                        Else
                            sOut = sOut & "," & QSTR & _
                            Replace(myField.Text, QSTR, QSTR & QSTR) & QSTR
                        End If
                    End If
            Next myField
            Print #nFileNum, Mid(sOut, 1)
            sOut = Empty
        End With
    Next myRecord
    Close #nFileNum
End Sub

This code will strip away the extra quotations and leave the data with just single quotes at the beginning and end of the data. I hope this helps someone and if I have done something wrong in my format or not displayed the data correctly, forgive me. I am just trying to help others. Bear in mind, this isn't my code at all. I just got it working for me and I suspect others are trying to get what I achieved here. The original code can be found here http://www.mcgimpsey.com/excel/textfiles.html#csvwithquotes



来源:https://stackoverflow.com/questions/13969254/unwanted-double-quotes-in-generated-csv-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!