Atitit 命令行返回 cli 返回解析 tab分割csv格式 v4 t34.docx
Atitit 命令行返回 cli 返回解析 csv 格式 cliutil
目录
1.2. 只好自己读取,是有multibackspace分割 2
1.4. /bookmarksHtmlEverythingIndexPrj/src/com/attilax/util/shellUtilV2t33.java 2
private static void readAsCsv(String ps_rzt_csv) throws IOException {
// 创建CSV读对象
// CsvReader.parse(arg0)
CsvReader csvReader = new CsvReader(IOUtils.toInputStream(ps_rzt_csv), '\t',Charset.defaultCharset());
//CsvReader.parse(ps_rzt_csv);
// 读表头
// csvReader.readHeaders();
while (csvReader.readRecord()) {
System.out.println("--getColumnCount:" + csvReader.getColumnCount());
System.out.println(csvReader.get(5));
// 读一整行
// System.out.println(csvReader.getRawRecord());
// 读这行的某一列
// System.out.println(csvReader.get("Link"));
}
}
public class shellUtilV2t33 {
public static List<Map> parse(String result) {
List<Map> tab =shellUtilV2t33.toTableNoHeadMode_ByMultiSpace(result);
//csvService.toTableByTab(result);;
return tab;
}
public static List<Map> toTableNoHeadMode_ByMultiSpace(String csv) {
List<Map> li = Lists.newArrayList();
String[] rows = csv.split("\n");
int idx = 0;
for (String row : rows) {
row=row.trim();
if(row.length()==0)
continue;
Map m = Maps.newLinkedHashMap();
String[] cols = row.split(" ");
List<String> cols_li=getcols_li(cols);
for (int j = 0; j < cols_li.size(); j++) {
m.put("index" + String.valueOf(j), cols_li.get(j));
}
li.add(m);
}
return li;
}
private static List<String> getcols_li(String[] cols) {
List<String> stringA = Arrays.asList(cols);
List<String> li= Listutil.delEmptyElement(stringA);
return li;
}
package com.attilax.util;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import com.attilax.data.csv.csvService;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
public class shellUtil {
public static List<Map> parse(String result) {
List<Map> tab =toTableNoHeadMode_ByMultiSpace(result);
//csvService.toTableByTab(result);;
return tab;
}
public static List<Map> toTableNoHeadMode_ByMultiSpace(String csv) {
List<Map> li = Lists.newArrayList();
String[] rows = csv.split("\n");
int idx = 0;
for (String row : rows) {
row=row.trim();
if(row.length()==0)
continue;
Map m = Maps.newLinkedHashMap();
String[] cols = row.split(" ");
List<String> cols_li=getcols_li(cols);
for (int j = 0; j < cols_li.size(); j++) {
m.put("index" + String.valueOf(j), cols_li.get(j));
}
li.add(m);
}
return li;
}
private static List<String> getcols_li(String[] cols) {
List<String> stringA = Arrays.asList(cols);
List<String> li= Listutil.delEmptyElement(stringA);
return li;
}
public static List<Map> parse_netstatNfindstr(String result) {
List<Map> tab =toTableNoHeadMode_ByMultiSpace(result);
tab.remove(0);
return csvService.toTableBySpace_firstlineTitle(tab);
}
}