简体   繁体   English

读取Java中具有可变数据类型的CSV

[英]Read CSVs in java with variable data types

I am building a generic tool in Java which reads data from a CSV and assigns it to some variables in the code and does bunch of stuff with it. 我正在用Java构建一个通用工具,该工具可以从CSV读取数据并将其分配给代码中的某些变量,并对其进行大量处理。 I just realised that I should use an existing library (like SuperCSV) than doing it on my own. 我只是意识到我应该使用现有的库(例如SuperCSV),而不是自己做。 Now my question is the following: Since this is a generic tool, the datatypes of the values are not known. 现在我的问题是:由于这是一个通用工具,因此值的数据类型未知。 It depends what is present in the CSVfile. 这取决于CSV文件中的内容。 Now the user who would be building the CSV would know the datatypes. 现在,将要构建CSV的用户将知道数据类型。 So I could add a "datatype" column in the CSV and the user could write "int" or "float" or "String" there. 因此,我可以在CSV中添加“数据类型”列,并且用户可以在其中写入“ int”或“ float”或“ String”。 And then I could use if/then clauses in the code to assign the right datatype. 然后,我可以在代码中使用if / then子句来分配正确的数据类型。 But I am not sure if that is the most apt approach. 但是我不确定这是否是最合适的方法。 Any thoughts? 有什么想法吗? (I dont post on StackOverflow often so please advise if I am not being clear or detailed enough. Thanks!) (我通常不会在StackOverflow上发帖,所以请告知我是否不清楚或不够详细。谢谢!)

regards Anupam 问候阿努帕姆

This does not look too effective to pratically define data type before each data item in a CSV-like data processing. 在类CSV数据处理中的每个数据项之前先于实际定义数据类型看起来似乎不太有效。 I would suggest to ask the CSV creator to specify the data types of each column beforehand in a predefined manner (say, specify a separate one-line CSV file with each data type, cause I think mixing raw data with metadata is not a good idea in this case). 我建议要求CSV创建者预先以预定义的方式指定每列的数据类型(例如,为每种数据类型指定一个单独的单行CSV文件,因为我认为将原始数据与元数据混合不是一个好主意)在这种情况下)。 In my experience there was a similar task to process several types measurement tool data and raw data CSV files were as-is, metadata was specified in XML, but was not processed with each file. 以我的经验,有一个类似的任务要处理几种类型的测量工具数据,原始数据CSV文件保持原样,元数据是用XML指定的,但未对每个文件进行处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM