- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有一个 csv 文件。我想从中提取特定的列。例如:说,我有 csv:
id1,caste1,salary,name1
63,Graham,101153.06,Abraham
103,Joseph,122451.02,Charlie
63,Webster,127965.91,Violet
76,Smith,156150.62,Eric
97,Moreno,55867.74,Mia
65,Reynolds,106918.14,Richard
我如何使用 opencsv 只读取来自 header caste1 的数据?
最佳答案
Magnilex 和 Sparky 是正确的,CSVReader 不支持按列名读取值。但话虽如此,您可以通过两种方式做到这一点。
假设您有列名并且默认的 CSVReader 读取标题,您可以首先搜索标题的位置,然后从那里开始使用它;
private int getHeaderLocation(String[] headers, String columnName) {
return Arrays.asList(headers).indexOf(columnName);
}
所以你的方法看起来像(省略了很多你需要放入的错误检查)
CSVReader reader = new CSVReader(new FileReader("yourfile.csv"));
String [] nextLine;
int columnPosition;
nextLine = reader.readNext();
columnPosition = getHeaderLocation(nextLine, "castle1");
while ((nextLine = reader.readNext()) != null && columnPosition > -1) {
// nextLine[] is an array of values from the line
System.out.println(nextLine[columnPosition]);
}
只有在您时间紧迫且您只关心一个专栏时,我才会执行上述操作。这是因为 openCSV 可以使用 CsvToBean
类和 HeaderColumnNameMappingStrategy
直接转换为具有与标题列名称相同的变量的对象。
因此,首先您将定义一个包含字段的类(实际上您只需要放入您想要的字段——多余的字段将被忽略,缺失的字段为 null 或默认值)。
public class CastleDTO {
private int id1;
private String castle1;
private double salary;
private String name1;
// have all the getters and setters here....
}
那么你的代码看起来像
CSVReader reader = new CSVReader(new FileReader("yourfile.csv"));
HeaderColumnNameMappingStrategy<CastleDTO> castleStrategy = new HeaderColumnNameMappingStrategy<CastleDTO>();
CsvToBean<CastleDTO> csvToBean = new CsvToBean<CastleDTO>();
List<CastleDTO> castleList = csvToBean.parse(castleStrategy, reader);
for (CastleDTO dto : castleList) {
System.out.println(dto.getCastle1());
}
关于java - 如何读取 opencsv 中的特定 header ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31414120/
我使用了 opencsv 的“http://opencsv.sourceforge.net/#maven-integration ” maven 依赖项,如下所示: net.sf.openc
我正在尝试使用 HeaderColumnNameMappingStrategy 使用 opencsv 读取 csv 文件。 Opencsv 每次都将我的第一列 csv 填充为 null。 这是我的代码
使用 OpenCSV 库,调用 StatefulBeanToCsv.write()我的空值被包裹在引号中。 例子: String[] columns = new String[] { "Col
我正在使用 AndroidStudio,我的应用程序必须读取如下所示的 CSV 文件: "Anmeldung";"1576017126809898";"1547126680978123";"";"";
在我的主课 public static void main(String[] args) { CSVWriter writer = new CSVWriter(new Fi
我使用openCSV导入文件,但通常它只读取文件的一部分。它处理包含约 5k 行的文件,但通常 20k 对他来说是个问题(即使是 1 列的 CSV)。 当我尝试使用 BufferedReader 而不
我试图使用 openCSV java 库在 csv 文件中写入一些公式数据,但它只写入每个单元格中大约 90% 的字符串。是否有任何已知问题可能导致此行为? public static void ma
我正在使用 opencsv 2.3,它似乎没有像我预期的那样处理转义字符。我需要能够处理不使用引号字符的 CSV 文件中的转义分隔符。 示例测试代码: CSVReader reader = new C
我试图从分号分隔的文件中的倒数第二行和第三列获取值。我无法坐下来获取倒数第二行和第三列的值。 我一直在寻找一种方法来实现这一目标,但没有结果。如果有人能用一个例子为我指明正确的方向,我将不胜感激。 这
我正在尝试使用 opencsv 解析 csv 文件。文件中的最后一列有一个参数,实际上是 json。该 json 包含在“”中。问题是 opencsv 正在从 json 中删除一些 "导致我的代码中断
我使用 open csv 来读取只有 2 列的 CSV,例如: "valueA1","valueB of A1" ,"valueB of A1" ,"valueB of A1" ,"valueB of
我陷入了必须读取两个不同的 csv 文件并将它们存储在单个表中的情况。 CSV 文件1: salary,date,user 121,2016-08-01,admin 121,2016-08-01,ad
我有一个 CSV 文件,其中引号内有定界符或未闭合的引号,如何让 CSVReader 忽略引号内的引号和定界符。例如: 123|Bhajji|Maga|39|"I said Hey|" I am "5
我使用的是 opencsv 3.0 版本。由于某种原因,它没有将记录写出到 CSV 文件中。它适用于 2.3 版。但是,我想使用 CSVIterator 类。感谢您的帮助。 public void
我正在尝试使用 opencsv 解析一些公共(public)数据(3.10 版)。下面是获取 CSV 并将记录映射到 POJO 列表的代码片段: URL permitsURL = new URL("h
好的,我有一个 csv 文件: Summary heading1,heading2,heading3 value1,value2,value2 value1,value2,va
我需要有关在 Java 中解析 CSV 数据时遇到的问题的建议。 我有一个 CSV 文件,其中包含以下格式的数据 name, gender, address_first_line, address_s
我编写了一个脚本,使用 tokenize 在 groovy 中解析 .csv 文件,但最终没有完全满足我的需要,我正在尝试使用 openCSV 库,但我不确定如何解析各个列。这是到目前为止我的代码:
我正在使用 openCSV 的 CsvToBean 类。该 bean 具有日期字段。 @CsvDate(value = "yyyy-MM-dd") @CsvBindByPosition(positio
我有以下 CSV 文件, "id","Description","vale" 1,New"Account","val1" 我无法使用 OpenCSV 读取上述 CSV 文件。它无法读取 New"Acc
我是一名优秀的程序员,十分优秀!