Groovy OpenCSV 读取带反斜杠的值(例如 "domain\user" )
Groovy OpenCSV read value with backslash (e.g. "domain\user" )
在 Groovy 中,我使用 opencsv 解析 CSV 文件。我的代码没有使用反斜杠处理值。
我的输入文件有这个值
value1,domain\user,value2
这是我的 groovy 代码。
def filename = 'C:\Temp\list.txt'
CSVReader csvReader = new CSVReader(new FileReader(filename))
String[] nextRecord
while ((nextRecord = csvReader.readNext()) != null) {
println nextRecord
}
csvReader.close()
它为第二个字段打印不带反斜杠的值。
[value1, domainuser, value2]
如何处理 OpenCSV 中的反斜杠值?
谢谢
SR
=============
Apache Common 解析器工作正常。
Iterable<CSVRecord> records = CSVFormat.EXCEL.parse(new FileReader(filename));
for (CSVRecord record : records) {
String f1 = record.get(0);
String f2 = record.get(1);
String f3 = record.get(2);
println f1
println f2
println f3
}
在 3.9 版本中,除了 CSVParser 之外,opencsv 还引入了另一种解析器。解析器是 CSVReader 的基础。这个新的解析器叫做 RFC4180Parser。正如official documentation所述
RFC4180 defines a standard for all of the nitty-gritty questions of just precisely how CSV files are to be formatted...
The main difference between between the CSVParser and the RFC4180Parser is that the CSVParser uses an escape character to denote "unprintable" characters while the RFC4180 spec takes all characters between the first and last quote as gospel (with the exception of the double quote which is escaped by a double quote).
所以请尝试使用 opencsv 3.9+ 和 RFC4180Parser。对我有用
def parser = new RFC4180ParserBuilder().build()
def reader = new CSVReaderBuilder(new FileReader(filename)).withCSVParser(parser).build();
println reader.readNext()
输出:
[value1, domain\user, value2]
如果由于某种原因您不能使用 3.9 及更高版本,您可以设置旧的解析器,以便转义字符是其他字符而不是反斜杠。但在这种情况下,如果原始文件的创建者根据 official documentation
使用反斜杠作为转义字符,则存在破坏文件其他行的风险
... Sometimes the separator character is included in the data for a field itself, so quotation characters are necessary. Those quotation characters could be included in the data also, so an escape character is necessary...
所以我的建议是使用版本 3.9+ 和 RFC4180Parser
在 Groovy 中,我使用 opencsv 解析 CSV 文件。我的代码没有使用反斜杠处理值。
我的输入文件有这个值
value1,domain\user,value2
这是我的 groovy 代码。
def filename = 'C:\Temp\list.txt'
CSVReader csvReader = new CSVReader(new FileReader(filename))
String[] nextRecord
while ((nextRecord = csvReader.readNext()) != null) {
println nextRecord
}
csvReader.close()
它为第二个字段打印不带反斜杠的值。
[value1, domainuser, value2]
如何处理 OpenCSV 中的反斜杠值?
谢谢 SR
============= Apache Common 解析器工作正常。
Iterable<CSVRecord> records = CSVFormat.EXCEL.parse(new FileReader(filename));
for (CSVRecord record : records) {
String f1 = record.get(0);
String f2 = record.get(1);
String f3 = record.get(2);
println f1
println f2
println f3
}
在 3.9 版本中,除了 CSVParser 之外,opencsv 还引入了另一种解析器。解析器是 CSVReader 的基础。这个新的解析器叫做 RFC4180Parser。正如official documentation所述
RFC4180 defines a standard for all of the nitty-gritty questions of just precisely how CSV files are to be formatted...
The main difference between between the CSVParser and the RFC4180Parser is that the CSVParser uses an escape character to denote "unprintable" characters while the RFC4180 spec takes all characters between the first and last quote as gospel (with the exception of the double quote which is escaped by a double quote).
所以请尝试使用 opencsv 3.9+ 和 RFC4180Parser。对我有用
def parser = new RFC4180ParserBuilder().build()
def reader = new CSVReaderBuilder(new FileReader(filename)).withCSVParser(parser).build();
println reader.readNext()
输出:
[value1, domain\user, value2]
如果由于某种原因您不能使用 3.9 及更高版本,您可以设置旧的解析器,以便转义字符是其他字符而不是反斜杠。但在这种情况下,如果原始文件的创建者根据 official documentation
使用反斜杠作为转义字符,则存在破坏文件其他行的风险... Sometimes the separator character is included in the data for a field itself, so quotation characters are necessary. Those quotation characters could be included in the data also, so an escape character is necessary...
所以我的建议是使用版本 3.9+ 和 RFC4180Parser