[英]How to read a csv and create a list of maps out of each line?
I have a Java class to automate some behaviour on the web, my only problem is that now instead of the static data that I have I need to use the data from the csv.我有一个 Java 类来自动化网络上的某些行为,我唯一的问题是现在我需要使用来自 csv 的数据而不是静态数据。
for example:例如:
this is one of the actions in my automation class:这是我的自动化课程中的操作之一:
WebElement supplierAddressField = driver.findElement(By.id("FieldaddressOfSupplierLine"));
supplierAddressField.sendKeys("hollywood blvd 34");
So now, instead of the static "supplier address" value I want to iterate on each line of the .sendKeys(csvLineMap.get("supplier address"));
所以现在,我想迭代.sendKeys(csvLineMap.get("supplier address"));
每一行,而不是静态的“供应商地址”值.sendKeys(csvLineMap.get("supplier address"));
Because in each line I dont need all the headers info, this is why I think it will be the best to just create a list of maps, that each map key will be the header of the csv and the value will be the value for this header in a specific line.因为在每一行中我不需要所有的标题信息,这就是为什么我认为最好只创建一个地图列表,每个地图键将是 csv 的标题,而值将是这个值特定行中的标题。
this is the structure of the csv:这是csv的结构:
Please help me to figure this out...thanksss!!请帮我解决这个问题……谢谢!!
For what you are asking for I would recommend you look at Apache Commons CSV .对于您的要求,我建议您查看Apache Commons CSV 。 One of the examples from their User Guide matches very closely with with the examples you are trying他们的 用户指南中的示例之一与您正在尝试的示例非常匹配
Reader in = new FileReader("path/to/file.csv");
Iterable<CSVRecord> records = CSVFormat.EXCEL.parse(in);
for (CSVRecord record : records) {
String lastName = record.get("Last Name");
String firstName = record.get("First Name");
}
ok, this might be overly complex for what you want, but I always open csv's as excel files because then you can run down the columns.好的,这对于您想要的东西来说可能过于复杂,但我总是将 csv 文件作为 excel 文件打开,因为这样您就可以向下运行这些列。 The code for picking up any column would look like this:获取任何列的代码如下所示:
Workbook w = Workbook.getWorkbook(inputWorkbook);
Sheet sheet = w.getSheet(0);
nom = sheet.getRows();
String[][] SheetArray = new String [2][nom];
// change the first number to the number of columns you want,
// or pick up the number same as you did with rows
Cell cell;
// GETS DATA FROM SHEET AND RUNS THROUGH WHOLE LOOP BELOW FOR EACH REFERENCE
for(int j =0;j<sheet.getRows();j++) // cycles through rows and loads into 2d array
{ // start 6
cell = sheet.getCell(0, j); <- your column number here
cellcont = cell.getContents();
SheetArray[0][j] = cellcont;
// repeat the above block for each column you want
} // end 6
you now have a 2d array with all the info in it which you can handle however you want.您现在拥有一个包含所有信息的二维数组,您可以根据需要进行处理。
wrap the entire thing in a try .. catch.将整个事情包裹在 try .. catch 中。
With uniVocity-parsers you can parse only the fields you are interested, in any order:使用uniVocity-parsers,您可以仅以任何顺序解析您感兴趣的字段:
CsvParserSettings parserSettings = new CsvParserSettings();
// Let's extract headers
parserSettings.setHeaderExtractionEnabled(true);
parserSettings.selectFields("Field 5", "Field 1");
//Rows will come organized according to your field selection
List<String[]> allRows = parser.parseAll("path/to/file.csv");
If you prefer, you can easily get a map with the values of all columns:如果您愿意,您可以轻松获得包含所有列值的映射:
CsvParserSettings parserSettings = new CsvParserSettings();
// Let's extract headers
parserSettings.setHeaderExtractionEnabled(true);
// To get the values of all columns, use a column processor
ColumnProcessor rowProcessor = new ColumnProcessor();
parserSettings.setRowProcessor(rowProcessor);
CsvParser parser = new CsvParser(parserSettings);
//This will kick in our column processor
parser.parse(new FileReader("path/to/file.csv"));
//Finally, we can get the column values:
Map<String, List<String>> columnValues = rowProcessor.getColumnValuesAsMapOfNames();
Have a look.看一看。 It is faster than any other parser and you can do much more, such as converting the values and generating java beans.它比任何其他解析器都快,您可以做更多的事情,例如转换值和生成 java bean。
Disclosure: I am the author of this library.披露:我是这个图书馆的作者。 It's open-source and free (Apache V2.0 license).它是开源且免费的(Apache V2.0 许可)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.