我有一个从数据库中卸载的文件,无论列的实际内容如何,所有 varchar 列都被引号包围(不幸的是,卸载过程超出了我的控制)。
像这样:
1,"Alex ,/,awesome/,","chan"
2,"Peter ,boring","pitt"
在 pom 中使用以下代码与 univocity 2.2.3 时:
public class Sample {
public static void main(String[] args) throws IOException {
BeanListProcessor<Person> rowProcessor = new BeanListProcessor<Person>(Person.class);
CsvParserSettings parserSettings = new CsvParserSettings();
parserSettings.setProcessor(rowProcessor);
parserSettings.getFormat().setDelimiter(',');
parserSettings.getFormat().setQuote('"');
parserSettings.getFormat().setQuoteEscape('/');
CsvParser parser = new CsvParser(parserSettings);
parser.parse(new FileReader("src/main/resources/person.csv"));
List<Person> beans = rowProcessor.getBeans();
Writer outputWriter = new FileWriter("src/main/resources/personOut.csv", true);
CsvWriterSettings settings = new CsvWriterSettings();
settings.getFormat().setDelimiter(',');
settings.getFormat().setQuote('"');
settings.getFormat().setQuoteEscape('/');
settings.getFormat().setCharToEscapeQuoteEscaping('\0');
settings.setRowWriterProcessor(new BeanWriterProcessor<Person>(Person.class));
CsvWriter writer = new CsvWriter(outputWriter, settings);
for (Person person : beans) {
writer.processRecord(person);
}
writer.close();
}
}
只有包含分隔符的列用引号括起来:
1,"Alex ,/,awesome/,",chan
2,"Peter ,boring",pitt
在编写器设置上使用settings.setQuoteAllFields(true);
时,所有字段都被引号包围,但现在非 varchar 字段有问题。
无论列的内容如何(例如,分隔符存在或不存在),我如何只用引号将源中的引号包围的列括起来?
期望的结果:
1,"Alex ,/,awesome/,","chan"
2,"Peter ,boring","pitt"