java OpenCSV 没有转义引号(“)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30757940/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
OpenCSV not escaping the quotes(")
提问by BhajjiMaga
I have a CSV file which will have delimiter or unclosed quotes inside a quotes, How do i make CSVReader ignore the quotes and delimiters inside quotes. For example:
我有一个 CSV 文件,它在引号内有分隔符或未闭合的引号,如何让 CSVReader 忽略引号内的引号和分隔符。例如:
123|Bhajji|Maga|39|"I said Hey|" I am "5|'10."|"I a do "you"|get that"
This is the content of file.
这是文件的内容。
The below program to read the csv file.
下面的程序读取 csv 文件。
@Test
public void readFromCsv() throws IOException {
FileInputStream fis = new FileInputStream(
"/home/netspurt/awesomefile.csv");
InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
CSVReader reader = new CSVReader(isr, '|', '\"');
for (String[] row; (row = reader.readNext()) != null;) {
System.out.println(Arrays.toString(row));
}
reader.close();
isr.close();
fis.close();
}
I get the o/p something like this.
我得到这样的o/p。
[123, Bhajji, Maga, 39, I said Hey| I am "5|'10., I am an idiot do "you|get that]
what happened to quote after you
引用之后发生了什么 you
Edit: The Opencsv dependency com.opencsv opencsv 3.4
编辑:Opencsv 依赖项 com.opencsv opencsv 3.4
采纳答案by BhajjiMaga
As the CSV format specifies the quotes(") if its inside a field we need to precede it by another quote("). So this solved my problem.
由于 CSV 格式指定了引号 ("),如果它在一个字段内,我们需要在它前面加上另一个引号 (")。所以这解决了我的问题。
123|Bhajji|Maga|39|"I said Hey|"" I am ""5|'10."|"I a do ""you""|get that"
Refrence: https://www.ietf.org/rfc/rfc4180.txt
回答by Remigius Stalder
from the source code of com.opencsv:opencsv:
来自 com.opencsv:opencsv 的源代码:
/**
* Constructs CSVReader.
*
* @param reader the reader to an underlying CSV source.
* @param separator the delimiter to use for separating entries
* @param quotechar the character to use for quoted elements
* @param escape the character to use for escaping a separator or quote
*/
public CSVReader(Reader reader, char separator,
char quotechar, char escape) {
this(reader, separator, quotechar, escape, DEFAULT_SKIP_LINES, CSVParser.DEFAULT_STRICT_QUOTES);
}
see http://sourceforge.net/p/opencsv/source/ci/master/tree/src/main/java/com/opencsv/CSVReader.java
见http://sourceforge.net/p/opencsv/source/ci/master/tree/src/main/java/com/opencsv/CSVReader.java
There is a constructor with an additional parameter escape which allows to escape separators and quotes (as per the javadoc).
有一个带有附加参数转义的构造函数,它允许转义分隔符和引号(根据 javadoc)。
回答by Scott Conway
Sorry but I don't have enough rep to add a comment so I will have to add an answer.
抱歉,我没有足够的代表来添加评论,所以我必须添加一个答案。
For your original question of what happened to the quote after the you the answer is the same as what happened to the quote before the I.
对于您最初的问题,即在您之后的引用发生了什么情况,答案与在 I 之前的引用发生的情况相同。
For CSV data the quote immediately before and after the separator is the start and end of the field data and is thus removed. That is why those two quotes are missing.
对于 CSV 数据,分隔符前后的引号是字段数据的开始和结束,因此被删除。这就是为什么缺少这两个引号的原因。
回答by Scott Conway
You need to escape out the quotes that are part of the field. The default escape character is the \
您需要转义字段中的引号。默认转义字符是 \
Taking a guess as to which quotes you want to escape the string should look like
猜测你想转义字符串的引号应该是这样的
123|Bhajji|Maga|39|"I said \"Hey I am \"5'10. Do \"you\" get that?\""