Super CSV (Java) - 读取列名中带有空格的文件
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您会注意到链接到的示例中的这一行:
这应该为您提供列名称,不是吗?
http://supercsv.sourceforge.net/javadoc/index.html
我想我明白现在你的问题。传递给 read 函数的 String[] 参数采用您要读入的类的属性名称。它是位置性的,因此不必像标题那样命名。因此,例如,您可以拥有
String[] header = inFile.getCSVHeader()
,但随后拥有headerName->propertyName
的映射,因此,如果您的标头字段是:但是您的类被
传递给
read
方法,而不是(String[]) {"First Name", "Last Name", "Age"}
,因为您的header 是一个(String[]) {"FirstName", "LastName", "Years"}
数组,但传递给read
。You notice this line in the samples you link to:
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 haveString[] header = inFile.getCSVHeader()
, but then have a mapping ofheaderName->propertyName
, so, if your header fields were:but your class was
pass to the
read
method not(String[]) {"First Name", "Last Name", "Age"}
, as your header is, but pass toread
, a(String[]) {"FirstName", "LastName", "Years"}
array.使用带有空格的标头将 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);
如果您想使用不同的名称映射 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..
我意识到这是一篇相当老的帖子,但其他人可能正在寻找替代的简单解决方案。这就是我所做的,以确保我只有字母数字标题名称,没有空格:
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: