如何通过 Lambda 函数重命名列 - fasterXML
How to rename Columns via Lambda function - fasterXML
我正在使用 FasterXML library 来解析我的 CSV file
。 CSV file
的第一行有列名。不幸的是,我需要重命名这些列。我有一个 lambda 函数,我可以在其中传递 csv file
中的红色值并获取新值。
我的代码看起来像这样,但不起作用。
CsvSchema csvSchema =CsvSchema.emptySchema().withHeader();
ArrayList<HashMap<String, String>> result = new ArrayList<HashMap<String, String>>();
MappingIterator<HashMap<String,String>> it = new CsvMapper().reader(HashMap.class)
.with(csvSchema )
.readValues(new File(fileName));
while (it.hasNext())
result.add(it.next());
System.out.println("changing the schema columns.");
for (int i=0; i < csvSchema.size();i++) {
String name = csvSchema.column(i).getName();
String newName = getNewName(name);
csvSchema.builder().renameColumn(i, newName);
}
csvSchema.rebuild();
当我稍后尝试打印这些列时,它们仍然与我 CSV file
.
的第一行相同
另外我注意到,csvSchema.size()
等于 0
- 为什么?
您可以改为使用 uniVocity-parsers。以下解决方案将输入行流式传输到输出,因此您无需将所有内容加载到内存中,然后使用新 headers 写回数据。会快很多:
public static void main(String ... args) throws Exception{
Writer output = new StringWriter(); // use a FileWriter for your case
CsvWriterSettings writerSettings = new CsvWriterSettings(); //many options here - check the documentation
final CsvWriter writer = new CsvWriter(output, writerSettings);
CsvParserSettings parserSettings = new CsvParserSettings(); //many options here as well
parserSettings.setHeaderExtractionEnabled(true); // indicates the first row of the input are headers
parserSettings.setRowProcessor(new AbstractRowProcessor(){
public void processStarted(ParsingContext context) {
writer.writeHeaders("Column A", "Column B", "... etc");
}
public void rowProcessed(String[] row, ParsingContext context) {
writer.writeRow(row);
}
public void processEnded(ParsingContext context) {
writer.close();
}
});
CsvParser parser = new CsvParser(parserSettings);
Reader reader = new StringReader("A,B,C\n1,2,3\n4,5,6"); // use a FileReader for your case
parser.parse(reader); // all rows are parsed and submitted to the RowProcessor implementation of the parserSettings.
System.out.println(output.toString());
//nothing else to do. All resources are closed automatically in case of errors.
}
如果您想要 reorder/eliminate 列,您可以使用 parserSettings.selectFields("B", "A")
轻松 select 列。
披露:我是这个图书馆的作者。它 open-source 并且免费(Apache V2.0 许可)。
我正在使用 FasterXML library 来解析我的 CSV file
。 CSV file
的第一行有列名。不幸的是,我需要重命名这些列。我有一个 lambda 函数,我可以在其中传递 csv file
中的红色值并获取新值。
我的代码看起来像这样,但不起作用。
CsvSchema csvSchema =CsvSchema.emptySchema().withHeader();
ArrayList<HashMap<String, String>> result = new ArrayList<HashMap<String, String>>();
MappingIterator<HashMap<String,String>> it = new CsvMapper().reader(HashMap.class)
.with(csvSchema )
.readValues(new File(fileName));
while (it.hasNext())
result.add(it.next());
System.out.println("changing the schema columns.");
for (int i=0; i < csvSchema.size();i++) {
String name = csvSchema.column(i).getName();
String newName = getNewName(name);
csvSchema.builder().renameColumn(i, newName);
}
csvSchema.rebuild();
当我稍后尝试打印这些列时,它们仍然与我 CSV file
.
另外我注意到,csvSchema.size()
等于 0
- 为什么?
您可以改为使用 uniVocity-parsers。以下解决方案将输入行流式传输到输出,因此您无需将所有内容加载到内存中,然后使用新 headers 写回数据。会快很多:
public static void main(String ... args) throws Exception{
Writer output = new StringWriter(); // use a FileWriter for your case
CsvWriterSettings writerSettings = new CsvWriterSettings(); //many options here - check the documentation
final CsvWriter writer = new CsvWriter(output, writerSettings);
CsvParserSettings parserSettings = new CsvParserSettings(); //many options here as well
parserSettings.setHeaderExtractionEnabled(true); // indicates the first row of the input are headers
parserSettings.setRowProcessor(new AbstractRowProcessor(){
public void processStarted(ParsingContext context) {
writer.writeHeaders("Column A", "Column B", "... etc");
}
public void rowProcessed(String[] row, ParsingContext context) {
writer.writeRow(row);
}
public void processEnded(ParsingContext context) {
writer.close();
}
});
CsvParser parser = new CsvParser(parserSettings);
Reader reader = new StringReader("A,B,C\n1,2,3\n4,5,6"); // use a FileReader for your case
parser.parse(reader); // all rows are parsed and submitted to the RowProcessor implementation of the parserSettings.
System.out.println(output.toString());
//nothing else to do. All resources are closed automatically in case of errors.
}
如果您想要 reorder/eliminate 列,您可以使用 parserSettings.selectFields("B", "A")
轻松 select 列。
披露:我是这个图书馆的作者。它 open-source 并且免费(Apache V2.0 许可)。