我使用默认构造函数和默认转义字符,即反斜杠。
因此,CSV格式与此"value1","value2","value3","value4"
从CSV文件Windows XP OS中读取值。
现在的问题是当这个值作为双引号时就像这个"aa"," I am "fine", what about u?", "232",
我不明白如何逃避双引号。
有人可以帮我吗?
答案 0 :(得分:1)
双引号字符
CSVParser csv = new CSVParser()
String[] result = csv.parseLine('"aa","I am ""fine"", what about u?", "232"')
assert result[0] == 'aa'
assert result[1] == 'I am "fine", what about u?'
assert result[2] == '232'
此答案摘自以下groovy测试文件 OpenCSVTest.groovy :
import groovy.util.GroovyTestCase
import au.com.bytecode.opencsv.CSVReader
import au.com.bytecode.opencsv.CSVParser
@Grapes([
@Grab(group='net.sf.opencsv', module='opencsv', version='2.3')
])
class OpenCSVTest extends GroovyTestCase {
void testParseQuote() {
CSVParser csv = new CSVParser()
String[] result = csv.parseLine('"aa","I am ""fine"", what about u?", "232"')
assert result[0] == 'aa'
assert result[1] == 'I am "fine", what about u?'
assert result[2] == '232'
}
}
运行如下:
$ groovy OpenCSVTest
.
Time: 0.009
OK (1 test)
答案 1 :(得分:0)
在你的问题中,你告诉你使用的是反斜杠的默认转义字符。所以你必须使用反斜杠......比如:"aa"," I am \"fine\", what about u?", "232"
从这样的代码中测试它:
CSVParser csvParser = new CSVParser();
String[] parseLine = csvParser.parseLine("\"aa\",\" I am \"\"fine\"\", what about u?\", \"232\"");