gpt4 book ai didi

java - uniVocity - 按位置转换为列表

转载 作者:太空宇宙 更新时间:2023-11-04 11:45:08 26 4
gpt4 key购买 nike

我正在尝试从 csv 文件中的特定位置(而不是括号)创建一个列表。

class Person {//
@Parsed
private String name;
@??
private Address address;
@Convert(conversionClass = Emails2.class, args = { "," , "5,6" })
@Parsed
private List<String> emails;
}

csv 格式:

name,email1,email2,email3,street,number,neighborhood
Maria,ma@gmail.com,ma@hotmail.com,,Regent Street,12,downtown
Ana,ana@gmail.com,a@hotmail.com,,Bird Street,,east side

我需要读取 csv 文件并创建电子邮件列表和地址对象。我试图使用@Convert,

public class Emails2 implements Conversion<String, Set<String>> {

private final String separator;
private final Set<String> positions;

public Emails2(String... args) {
String separator = ",";
Set<String> positions = null;

if (args.length == 1) {
separator = args[0];
}

if (args.length == 2) {
positions = new HashSet<String>();
String[] posi = args[1].split(",");
for (String string : posi) {
positions.add(string);
}
}
this.separator = separator;
this.positions = positions;
}

public Emails2(String separator, Set<String> positions) {
this.separator = separator;
this.positions = positions;
}
//this method is not called, I don't know why
public Set<String> execute(String input) { //I would like add the list and Address object inside this method to get it done in beanProcessed(...)
if (input == null) {
return Collections.emptySet();
}

Set<String> out = new TreeSet<String>();
for (String token : input.split(separator)) {
for (String word : token.trim().split("\\s")) {
out.add(word.trim());
}
}
return out;
}

public String revert(Set<String> input) {
return revert(input);
}

}

我怎么样

    public static void main(String[] args) {

BeanProcessor<Person> rowProcessor = new BeanProcessor<Person>(Person.class) {
@Override
public void beanProcessed(Person c, ParsingContext context) {
System.out.println(c.getName());

String[] positions = context.currentParsedContent().split(",");
System.out.println(positions[5]);
//I'm using fixed position here, I'd like get the position from @Convert or another way by configuration
System.out.println(positions[6]);
List<String> list = new ArrayList<>();
list.add(positions[5]);
list.add(positions[6]);
c.setEmails(list);
}

};

CsvParserSettings parserSettings = new CsvParserSettings();
parserSettings.setRowProcessor(rowProcessor);
parserSettings.setHeaderExtractionEnabled(true);
CsvParser parser2 = new CsvParser(parserSettings);
parser2.parse(getReader("/var/lib/cob/test2.csv"));

}

最佳答案

您尝试通过自定义转换将多列(email1email2email3)的值分配给单个属性 (Person.emails),但不支持这种做法,因为转换的目标是单个字段(或输入的列)而不是整行。

此外,自定义转换中的 execute 方法不会被调用,因为输入中没有 emails header 。当您使用 @Parsed 注释来注释属性而不提供要匹配的 header 名称时,将使用属性名称本身。如果您将属性从 emails 重命名为 email1,您将看到您的转换类将被调用,或者您只需在注释中提供字段名称,如下所示:

@Parsed(field = "email1")
private List<String> emails;

它也会被调用。

我明白你想在这里做什么并打开 this issue为此类需求提供更好的支持。

希望这有帮助。

关于java - uniVocity - 按位置转换为列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42426007/

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