我解析CSV文件并使用supercsv创建域对象.我的域对象有一个枚举字段,例如:
public class TypeWithEnum {
private Type type;
public TypeWithEnum(Type type) {
this.type = type;
}
public Type getType() {
return type;
}
public void setType(Type type) {
this.type = type;
}
}
Run Code Online (Sandbox Code Playgroud)
我的枚举看起来像这样:
public enum Type {
CANCEL, REFUND
}
Run Code Online (Sandbox Code Playgroud)
尝试使用此CSV文件创建bean:
final String[] header = new String[]{ "type" };
ICsvBeanReader inFile = new CsvBeanReader(new FileReader(
getFilePath(this.getClass(), "learning/enums.csv")), CsvPreference.STANDARD_PREFERENCE);
final CellProcessor[] processors =
new CellProcessor[]{ TODO WHAT TO PUT HERE? };
TypeWithEnum myEnum = inFile.read(
TypeWithEnum.class, header, processors); …
Run Code Online (Sandbox Code Playgroud) 我想使用SuperCSV CellProcessor用逗号作为小数分隔符(','而不是'.')解析double.
我想将第一个元素(0,35)解析为Double
0,35;40000,45
Run Code Online (Sandbox Code Playgroud)
我尝试过类似的东西:
/** FRENCH_SYMBOLS */
private static final DecimalFormatSymbols FRENCH_SYMBOLS = new DecimalFormatSymbols(Locale.FRANCE);
DecimalFormat df = new DecimalFormat();
df.setDecimalFormatSymbols(FRENCH_SYMBOLS);
final CellProcessor[] processors = new CellProcessor[] {
new NotNull(new ParseDouble(new FmtNumber(df))),
new NotNull(new ParseBigDecimal(FRENCH_SYMBOLS)) };
Run Code Online (Sandbox Code Playgroud)
ParseBigDecimal工作正常,但parseDouble似乎不起作用,它给了我一个异常:org.supercsv.exception.SuperCsvCellProcessorException:'0,35'无法解析为Double
我正在使用supercsv 2.1.0来解析其中包含德语单词的CSV文件.
给定的CSV文件在第一行有一个标题.在这个标题中有一些变异的元音,如:Ä,ä,Ü,ö等.例如:Betrag;Währung;信息
在我的编码中,我试图像这样得到csv的标题:
ICsvBeanReader inFile = new CsvBeanReader(new InputStreamReader(new FileInputStream(file), "UTF8"), CsvPreference.EXCEL_NORTH_EUROPE_PREFERENCE);
final String[] header = inFile.getHeader(true);
Run Code Online (Sandbox Code Playgroud)
这是我的标题数组的问题.使用utf8字符集不能正确编码带有变异元音的所有标题.
有没有办法正确读取标题?
这是一个伪单元测试:
public class TestSuperCSV {
@Test
public void test() {
String path = "C:\\Umsatz.csv";
File file = new File(path);
try {
ICsvBeanReader inFile = new CsvBeanReader(new InputStreamReader(
new FileInputStream(file), "UTF-8"),
CsvPreference.EXCEL_NORTH_EUROPE_PREFERENCE);
final String[] header = inFile.getHeader(true);
System.out.println(header[9]); //getting "W?hrung" but needed "Währung" here
} catch (UnsupportedEncodingException | FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
} …
Run Code Online (Sandbox Code Playgroud) 我想我发现了一个错误.或者可能不是,但Super CSV无法很好地处理.
我正在使用MapReader解析包含41列的CSV文件.但是,我正在获得CSV - 以及给我CSV的网络服务混乱了一行."标题"行是制表符分隔的行,包含41个单元格.
而"错误的行"是一个带有36个单元格的制表符分隔行,内容没有任何意义.
这是我正在使用的代码:
InputStream fis = new FileInputStream(pathToCsv);
InputStreamReader inReader = new InputStreamReader(fis, "ISO-8859-1");
ICsvMapReader mapReader = new CsvMapReader(inReader, new CsvPreference.Builder('"','\t',"\r\n").build());
final String[] headers = mapReader.getHeader(true);
Map<String, String> row;
while( (row = mapReader.read(headers)) != null ) {
// do something
}
Run Code Online (Sandbox Code Playgroud)
我在上面提到的行中执行mapReader.read(headers)时遇到异常.这是例外:
org.supercsv.exception.SuperCsvException:
the nameMapping array and the sourceList should be the same size (nameMapping length = 41, sourceList size = 36)
context=null
at org.supercsv.util.Util.filterListToMap(Util.java:121)
at org.supercsv.io.CsvMapReader.read(CsvMapReader.java:79)
at test.MyClass.readCSV(MyClass.java:20)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) …
Run Code Online (Sandbox Code Playgroud) 我有基于Java的Spring MVC应用程序,它也使用Spring安全性.我正在使用hibernate作为此Web应用程序的ORM工具.
以下是我的要求 -
用户可以使用Web浏览器上传CSV文件.已知CSV文件的格式包含以下5个字段:
userId, location, itemId, quantity, tranDate 001, NY, 00A8D5, 2, 12/31/2012 002, MN, 00A7C1, 10, 12/22/2012 . .
像这样大约有100行.
我在项目中使用Super CSV:
private void readWithCsvBeanReader(String CSV_FILENAME) throws Exception {
String CSV_FILENAME = "\\\\Serv01\\Files\\QueryResult.csv";
//String CSV_FILENAME = "C:\\Files\\QueryResult.csv";
ICsvBeanReader beanReader = null;
try {
beanReader = new CsvBeanReader(new FileReader(CSV_FILENAME),
CsvPreference.STANDARD_PREFERENCE);
// the header elements are used to map the values to the bean (names
// must match)
final String[] header = beanReader.getHeader(true);
// get Cell Processor
final CellProcessor[] …
Run Code Online (Sandbox Code Playgroud) 我有一个CSV文件有一些引用问题:
"Albanese Confectionery","157137","ALBANESE BULK ASST. MINI WILD FRUIT WORMS 2" 4/5LB",9,90,0,0,0,.53,"21",50137,"3441851137","5 lb",1,4,4,$6.7,$6.7,$26.8
Run Code Online (Sandbox Code Playgroud)
SuperCSV正在窒息这些水果虫(双关语).我知道2"
应该是2""
,但事实并非如此.LibreOffice实际上正确地解析了这个(让我感到惊讶).我正在考虑编写自己的小解析器,但其他行在字符串中有逗号:
"Albanese Confectionery","157230","ALBANESE BULK JET FIGHTERS,ASSORTED 4/5 B",9,90,0,0,0,.53,"21",50230,"3441851230","5 lb",1,4,4,$6.7,$6.7,$26.8
Run Code Online (Sandbox Code Playgroud)
有没有人知道Java库会处理这样的疯狂事情?或者我应该尝试所有可用的?或者我最好自己解决这个问题?
根据这篇关于SuperCSV能力的帖子,SuperCSV可以处理从数据库中读取的标题(仅列名)的值吗?
例如,以下代码段详细说明了当前状态和预期状态.
输入:
final String[] header = new String[] { "firstName", "lastName", "birthDate"};
// write the header
beanWriter.writeHeader(header);
// write the beans
for( final CustomerBean customer : customers ) {
beanWriter.write(customer, header, processors);
}
Run Code Online (Sandbox Code Playgroud)
OUTPUT:包含列名的文件:
firstName, lastName, birthDate
Bob , Doe , 02/12/2013
Run Code Online (Sandbox Code Playgroud)
输入:
final String[] header = new String[] { "firstName", "lastName", "birthDate"};
// write the header
beanWriter.writeHeader(header);
// write the beans
for( final CustomerBean customer : customers …
Run Code Online (Sandbox Code Playgroud) 我有一个带有String键和String值的hashmap.它包含大量的键及其各自的值.
例如:
key | value
abc | aabbcc
def | ddeeff
Run Code Online (Sandbox Code Playgroud)
我想将此hashmap写入csv文件,以便我的csv文件包含如下行:
abc,aabbcc
def,ddeeff
Run Code Online (Sandbox Code Playgroud)
我在这里使用supercsv库尝试了以下示例:http://javafascination.blogspot.com/2009/07/csv-write-using-java.html .但是,在此示例中,您必须为要添加到csv文件的每一行创建一个hashmap.我有大量的键值对,这意味着需要创建几个哈希映射,每个哈希映射包含一行的数据.我想知道是否有更优化的方法可用于此用例.
提前致谢!
此外,允许以可读/简单的方式处理长行,编写它们以及读取它们.
我已经研究过,opencsv
但它没有以任何顺序的方式引用行中的元素,并且supercsv
不允许按名称查看列,而只是查看数字.
另一个警告 - 我需要能够按名称来处理列 - 是列的数量不是常量,并且只有部分列标题是常量,如:
Name|Number|Color1|Color2......|Color67|
Run Code Online (Sandbox Code Playgroud)
并非任何给定的CSV文件中都存在1到67之间的所有颜色.
我正在寻找使用SuperCSV来检查我收到的一些文件的内容.文件的格式是这样的,它有一个标题记录,后跟数据记录,后跟CRC32校验和值.
例如
ABC|2|20130115150327|
1|1234567890123456|1234|20130109204710|21130109204710|
2|6543210987654321|1234|20130110043658|21130110043658|
1A345C7D
Run Code Online (Sandbox Code Playgroud)
关于SuperCSV在这种情况下的功能,我有几个问题.