gpt4 book ai didi

java - 如何通过 Lambda 函数重命名列 - fastXML

转载 作者:行者123 更新时间:2023-11-30 03:21:54 25 4
gpt4 key购买 nike

我正在使用FasterXML library解析我的 CSV 文件CSV 文件 的第一行包含列名称。不幸的是我需要重命名这些列。我有一个用于此目的的 lambda 函数,我可以在其中传递 csv 文件中的红色值并获取新值。

我的代码看起来像这样,但不起作用。

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 文件 的顶行中的相同。

另外我注意到,csvSchema.size() 等于 0 - 为什么?

最佳答案

您可以使用 uniVocity-parsers为了那个原因。以下解决方案将输入行流式传输到输出,因此您无需将所有内容加载到内存中,然后使用新 header 写回数据。它会快得多:

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.
}

如果您想要重新排序/删除列,可以使用 parserSettings.selectFields("B", "A") 轻松选择列。

披露:我是这个库的作者。它是开源且免费的(Apache V2.0 许可证)。

关于java - 如何通过 Lambda 函数重命名列 - fastXML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31145620/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com