生成带有一些值带引号,其他值不带引号的CSV文件

huangapple go评论70阅读模式
英文:

Generate CSV file with some values quoted and others not

问题

我是新手写CSV文件,遇到一个找不到答案的情况。我需要生成一个没有标题的CSV文件,其中数字值不带引号,字符串带引号。例如 - 71,72, "initial","2020-10-01"。

我使用了以下CSVWriter构造函数:

CSVWriter(FileWriter(ClassLoader.getSystemResource("mytable.csv").path), ',', '\'', '"', "\n")

我尝试将我的字符串值用双引号括起来,但这并不起作用。

val linesForCSVUploadToTable = arrayOf(
                    someId.toString(),
                    someOtherId.toString(),
                    "\"initial\"",
                    "\"2020-10-15\"",
                    "\"2020-10-15\"",
                    "\"csv\"",//created_by
                    "\"2020-10-15\"",
                    "\"csv\"",
                    "\"csv\"",
                    "\"2020-10-15\""
            )

输出结果是:

'100001','100001','""initial""','""2020-10-15""','""2020-10-15""','""csv""','""2020-10-15""','""csv""','""csv""','""2020-10-15""'

我必须将ids转换为字符串,因为writeAll()和writeNext()函数似乎接受一个String数组,而不接受ANY类型的数组。

我想避免编写自己的自定义CSV写入器。如果必须这样做,我也会这样做。

我使用的是OpenCSV 5.2,这是用Kotlin编写的。Java也可以使用。

任何帮助将不胜感激!谢谢。

英文:

I am new to writing CSV files and have a scenario I can't find answers on. I need to generate a CSV file with no headers where number values are not quoted, strings are quoted. An example would be - 71,72, "initial","2020-10-01".

I used the CSVWriter Constructor of:

CSVWriter(FileWriter(ClassLoader.getSystemResource("mytable.csv").path), ',', '\'', '\"', "\n")

I tried to populate my string values with double quotes as so but this does not work.

val linesForCSVUploadToTable = arrayOf(
                    someId.toString(),
                    someOtherId.toString(),
                    "\"initial\"",
                    "\"2020-10-15\"",
                    "\"2020-10-15\"",
                    "\"csv\"",//created_by
                    "\"2020-10-15\"",
                    "\"csv\"",
                    "\"csv\"",
                    "\"2020-10-15\""
            )

The output is:

'100001','100001','""initial""','""2020-10-15""','""2020-10-15""','""csv""','""2020-10-15""','""csv""','""csv""','""2020-10-15""'

I have to .toString the ids because the writeAll() and writeNext() functions appear to take a String Array and an array of type ANY isn't accepted.

I want to avoid writing my own custom CSV writer. If I have to then so be it.

I am using OpenCSV 5.2 and writing this in Kotlin. Java also works.

Any help would be much appreciated! Thanks

答案1

得分: 2

你可能应该使用Apache Commons CSV,它提供了NON_NUMERIC引用策略,因此会引用CSV文件中的任何非数字值。示例:

try (Writer w = ...) {
    CSVFormat outFormat = CSVFormat.newFormat(',')
        .withRecordSeparator("\n")
        .withQuote('"')
        .withQuoteMode(QuoteMode.MINIMAL);
    CSVPrinter printer = new CSVPrinter(w, outFormat);
    printer.printRecords(...);
}
英文:

You probably should use Apache Commons CSV, which provides the NON_NUMERIC quoting strategy and thus quotes any non-numeric value in your CSV file. Example:

try (Writer w = ...) {
    CSVFormat outFormat = CSVFormat.newFormat(',')
        .withRecordSeparator("\n")
        .withQuote('\"')
        .withQuoteMode(QuoteMode.MINIMAL);
    CSVPrinter printer = new CSVPrinter(w, outFormat);
    printer.printRecords(...);
}

huangapple
  • 本文由 发表于 2020年10月3日 05:00:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/64178208.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定