簡體   English   中英

如何在csv文件的String []數組中轉義逗號?

[英]How to escape the comma in the String[] array in a csv file?

我正在使用apache commons.csv.CSVparser。 我想例如在csv記錄中使用String數組:

"\"[\"54bb051e-3d12-11e5-91cd-b8f6b11b7feb\",\"472a9748-3d12-11e5-91cd-b8f6b11b7feb\"]\",Hallo,114058,Leon,31,\"     \",8400,bar,FOO";
        CSVParser csvParser = CSVFormat.DEFAULT
                .withDelimiter(CSV_SEPARATOR).withQuote(null)
                .withFirstRecordAsHeader()
                .parse(new StringReader(line));

如何在String []數組中轉義逗號? 讀完記錄后,字符串將拆分為一個Java數組。

我嘗試了這個:

@Test
    public void processLine() throws Exception {
        String line = "Ids,Info.name,Info.number,address.street,address.number,address.bus,address.postalcode,address.city," +
                "address.country\n" +
                "\"[\"\"54bb051e-3d12-11e5-91cd-b8f6b11b7feb\"\",\"\"472a9748-3d12-11e5-91cd-b8f6b11b7feb\"\"]\",Hallo,114058,Leon,31,\"     \",8400,foo,BAR";
        CSVParser csvParser = CSVFormat.DEFAULT
                .withDelimiter(CSV_SEPARATOR).withQuote(null)
                .withFirstRecordAsHeader()
                .parse(new StringReader(line));

String []的逗號仍然被視為分隔符。

您需要正確轉義CSV內容。 試試看: "\\"[\\"\\"54bb051e-3d12-11e5-91cd-b8f6b11b7feb\\"\\",\\"\\"472a9748-3d12-11e5-91cd-b8f6b11b7feb\\"\\"]\\",Hallo,114058,Leon,31,\\" \\",8400,bar,FOO"

轉義變得容易混淆,因為您將Java和CSV混合使用。 在Java中,您需要使用\\"來對雙引號進行轉義,而在CSV上,您需要對雙引號進行轉義。最后,您需要使用\\"\\"才能在字符串上獲取輸出"" 。最終的字符串將看起來像: "[""54bb051e-3d12-11e5-91cd-b8f6b11b7feb"",""472a9748-3d12-11e5-91cd-b8f6b11b7feb""]",Hallo,114058,Leon,31," ",8400,bar,FOO 。作為CSV上的第一個值: ["54bb051e-3d12-11e5-91cd-b8f6b11b7feb","472a9748-3d12-11e5-91cd-b8f6b11b7feb"]

另外,您的字符串不包含標頭,因此您需要注意withFirstRecordAsHeader()

這個:

CSVParser csvParser = CSVFormat.DEFAULT.withDelimiter(',').withQuote('"').parse(new StringReader(
        "\"[\"\"54bb051e-3d12-11e5-91cd-b8f6b11b7feb\"\",\"\"472a9748-3d12-11e5-91cd-b8f6b11b7feb\"\"]\",Hallo,114058,Leon,31,\"     \",8400,bar,FOO"));
System.out.println(csvParser.getRecords().get(0).get(0));

將輸出以下字符串:

["54bb051e-3d12-11e5-91cd-b8f6b11b7feb","472a9748-3d12-11e5-91cd-b8f6b11b7feb"]

並且可以使用此字符串將其解析為String []。

您不應該生成自己的CSV行進行測試,因為已經有了可以正確創建它的庫。 您曾想過使用Apache Commons讀取CSV而不創建它。

如果需要,使用CSVPrinter將“轉義”定界符(通過轉義,您將在格式允許的情況下雙引號)

//Get a printer on the System.out
CSVPrinter printer = CSVFormat.DEFAULT.withHeader("A", "B").printer();
// Create the pojos
List<POJO> pojos = new ArrayList<>();
pojos.add(new POJO("foo", "bar"));
pojos.add(new POJO("far", "boo"));
pojos.add(new POJO("for", "bao"));
pojos.add(new POJO("test,", "comma"));

for(POJO p : pojos) {
    printer.printRecord(p.a, p.b);
}

A,B
FOO,酒吧
到目前為止,噓
對於寶
“考”,逗號

使用POJO類

public class POJO{
    String a;
    String b;

    public POJO(String a, String b) {
        this.a = a;
        this.b = b;
    }

    @Override
    public String toString() {
        return "POJO [a=" + a + " ## b=" + b + "]";
    }
}

注意:這可能不是該庫的完美用法,我只使用過一次(現在),但這是向您展示可以/應該使用API​​來完成此操作,而不是創建自己的“ CSV”行

為了表明可以正確恢復,請使用Appendable存儲CSV:

StringBuffer sb = new StringBuffer();
CSVPrinter printer = CSVFormat.DEFAULT.withHeader("A", "B").print(sb);
List<POJO> pojos = new ArrayList<>();
pojos.add(new POJO("foo", "bar"));
pojos.add(new POJO("far", "boo"));
pojos.add(new POJO("for", "bao"));
pojos.add(new POJO("test,", "comma"));

for(POJO p : pojos) {
    printer.printRecord(p.a, p.b);
}

System.out.println("PRINTER");
System.out.println(sb.toString());

打印機
A,B
FOO,酒吧
到目前為止,噓
對於寶
“考”,逗號

然后解析該String並創建POJO back:

CSVParser parser = CSVFormat.DEFAULT
                .withFirstRecordAsHeader()
                .parse(new StringReader(sb.toString()));

System.out.println("PARSER");
parser.getRecords().stream().map(r -> new POJO(r.get(0), r.get(1))).forEach(System.out::println);

PARSER
POJO [a = foo ## b = bar]
POJO [a = far ## b = boo]
POJO [a = for ## b = bao]
POJO [a = test,## b =逗號

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM