Super CSV (Java) - 读取列名中带有空格的文件

发布于 2024-09-08 00:41:11 字数 376 浏览 3 评论 0原文

我正在使用 Super CSV,它看起来是一个很棒的包。

我唯一担心的是如何使用名称中带有空格的列。不,我不能回去亲自删除空格。这些文件将由数百个提供给我,我没有时间回去修复每个文件的所有 60 列,而且我不能相信其他人都能正确完成。

如何处理标题中带有空格的列(即“First Name”而不是“FirstName”或“firstName”)?

谢谢!

有关编码示例,请查看此处:http://supercsv.sourceforge.net/codeExamples_general.html

I'm working with Super CSV and it looks like an amazing package.

My only worry is how to work with columns with spaces in their names. No, I cannot go back and remove the spaces myself. These files will be given to me by the hundreds and I don't have time to go back and fix all 60 columns for each file and I can't trust everyone else to do it properly.

How can I work with columns with spaces in the title (i.e. "First Name" not "FirstName" or "firstName")?

Thanks!

For coding samples, look here: http://supercsv.sourceforge.net/codeExamples_general.html

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

悲喜皆因你 2024-09-15 00:41:11

您会注意到链接到的示例中的这一行:

final String[] header = inFile.getCSVHeader(true);

这应该为您提供列名称,不是吗?

http://supercsv.sourceforge.net/javadoc/index.html

我想我明白现在你的问题。传递给 read 函数的 String[] 参数采用您要读入的类的属性名称。它是位置性的,因此不必像标题那样命名。因此,例如,您可以拥有 String[] header = inFile.getCSVHeader(),但随后拥有 headerName->propertyName 的映射,因此,如果您的标头字段是:

First Name, Last Name, Age

但是您的类被

getFirstName(), setFirstName(...);
getLastName(), setLastName(...);
getYears(), setYears();

传递给 read 方法,而不是 (String[]) {"First Name", "Last Name", "Age"},因为您的header 是一个 (String[]) {"FirstName", "LastName", "Years"} 数组,但传递给 read

You notice this line in the samples you link to:

final String[] header = inFile.getCSVHeader(true);

This should give you your column names, no?

http://supercsv.sourceforge.net/javadoc/index.html

I think I understand your question now. The String[] argument passed to the read function takes the property names of the class you want to read into. It is positional, so it doesn't have to be named anything like the headers. So, for example, you can have String[] header = inFile.getCSVHeader(), but then have a mapping of headerName->propertyName, so, if your header fields were:

First Name, Last Name, Age

but your class was

getFirstName(), setFirstName(...);
getLastName(), setLastName(...);
getYears(), setYears();

pass to the read method not (String[]) {"First Name", "Last Name", "Age"}, as your header is, but pass to read, a (String[]) {"FirstName", "LastName", "Years"} array.

眼眸 2024-09-15 00:41:11

使用带有空格的标头将 bean 映射和标头数组分开。

private Final String[] header = new String[] { "名字", "姓氏"};
私有最终 String[] dataMapping = new String[] { "firstName", "lastName"};
beanWriter = new CsvBeanWriter(new FileWriter(path), CsvPreference.EXCEL_PREFERENCE);
beanWriter.writeHeader(标题);
beanWriter.write(yourPojo, dataMapping, 处理器);

To have a header with spaces separate the bean mapping and header array.

private final String[] header = new String[] { "First Name", "Last Name"};
private final String[] dataMapping = new String[] { "firstName", "lastName"};
beanWriter = new CsvBeanWriter(new FileWriter(path), CsvPreference.EXCEL_PREFERENCE);
beanWriter.writeHeader(header);
beanWriter.write(yourPojo, dataMapping, processors);

余厌 2024-09-15 00:41:11

如果您想使用不同的名称映射 CSV 标头,您可以创建一个哈希映射并将其用于内部实现,例如您可以将“First Name”映射为“firstName”,并根据您的内部名称填充您的 bean。

If you want to map CSV header with different name, you can create a hash map and use this for your internal implementation, e.g. you can map "First Name" with "firstName" and populate your bean based on your internal names..

国产ˉ祖宗 2024-09-15 00:41:11

我意识到这是一篇相当老的帖子,但其他人可能正在寻找替代的简单解决方案。这就是我所做的,以确保我只有字母数字标题名称,没有空格:

  final String[] header = beanReader.getHeader(true);

  for(int i = 0; i <= header.length -1 ; i++ ) {
    header[i] = header[i].replaceAll("[^a-zA-Z0-9]+","");
  }

I realize this is quite an old post but others may be looking for an alternative simple solution. This is what I did to ensure I had only alph numeric header names with no spaces:

  final String[] header = beanReader.getHeader(true);

  for(int i = 0; i <= header.length -1 ; i++ ) {
    header[i] = header[i].replaceAll("[^a-zA-Z0-9]+","");
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文