![](/img/trans.png)
[英]How to write list of objects in a csv using opencsv CSVWriter in java
[英]How to write to csv using CSVWriter from a comma separated key:value list of strings
我有一個 k:v 格式的字符串列表,如下所示:
List<String> data = new ArrayList();
data[0] = "acctHolderName:null,auctEndDt:2021-04-23,auctTitle:2 X BUCKLE RACING SEAT BELT HARNESS,bankName:null,buyerId:22494074,byrCntryId:1,cardlastFourDigits:1234,checkoutStatus:2,cityShipAddr:null,ckCompleteDate:2021-04-04 08:06:27,cnvstnIdList:null,countryShipAddr:null,createdTime:2021-04-04 08:06:27,firstTrackingDtlId:17824011,gmvBuyerLcAmt:59.990000000,gmvSellerLcAmt:59.9900000000000000,gmvUsdAmt:59.9900000000000000,instrumentType:null,itemId:4966893,itemPrice:59.99,itemSiteId:100,nonPciAccountId:null,omsExternalRfrncId:17-06848-86625,omsOrderId:170000684886625,orderStsId:0,paidDate:2021-04-04 08:06:27,paymentMethod:[34],"
data[1] = "acctHolderName:null,auctEndDt:2021-04-22,auctTitle:2 X BELT,bankName:null,buyerId:22491011,byrCntryId:2,cardlastFourDigits:5678,checkoutStatus:2,cityShipAddr:null,ckCompleteDate:2020-05-04 08:06:26,cnvstnIdList:null,countryShipAddr:null,createdTime:2020-05-04 08:06:27,firstTrackingDtlId:17834022,gmvBuyerLcAmt:59.990000000,gmvSellerLcAmt:59.9900000000000000,gmvUsdAmt:59.9900000000000000,instrumentType:null,itemId:4955893,itemPrice:59.99,itemSiteId:101,nonPciAccountId:null,omsExternalRfrncId:17-06848-86625,omsOrderId:170000684886625,orderStsId:0,paidDate:2021-03-04 08:06:27,paymentMethod:[34],"
如何使用opencsv CSVWriter
寫入 csv ,其中冒號之前是列名,之后是值。
因此,例如在上述情況下: acctHolderName:null
, acctHolderName
是列名, null 是它的值。
您可以使用方法CSVWriter.writeAll(List<String[]> list)
來編寫整個列表。 為了從您的列表中以所需格式List<String[]>
獲取適當的數據,您只需要提取標題,例如從列表的第一個元素中提取標題,以及從列表的所有元素中提取所需的所有值。 為此,只需將每個字符串拆分為,
, stream 到結果數組上,再次拆分:
map 到第一個元素作為列名,到第二個元素作為值。
例如,假設您的數據格式正確並且所有行都有相同數量的“列”,這是第一種沒有異常處理的方法:
public static void writeListToFile(List<String> list, String fileName) throws IOException {
List<String[]> out = new ArrayList<>();
//extract headers
String[] columns = Arrays.stream(list.get(0).split(","))
.map(str -> str.split(":")[0])
.toArray(String[]::new);
//extract values
List<String[]> rows = list.stream()
.map(line -> Arrays.stream(line.split(","))
.map(str -> str.split(":")[1])
.toArray(String[]::new))
.collect(Collectors.toList());
out.add(columns);
out.addAll(rows);
try (CSVWriter writer = new CSVWriter(new FileWriter(fileName))) {
writer.writeAll(out);
}
}
並調用上面的方法
public static void main(String[] args) throws IOException {
List<String> data = List.of("acctHolderName:null,auctEndDt:2021-04-23,auctTitle:2 X BUCKLE RACING SEAT BELT HARNESS,bankName:null,buyerId:22494074,byrCntryId:1,cardlastFourDigits:1234,checkoutStatus:2,cityShipAddr:null,ckCompleteDate:2021-04-04 08:06:27,cnvstnIdList:null,countryShipAddr:null,createdTime:2021-04-04 08:06:27,firstTrackingDtlId:17824011,gmvBuyerLcAmt:59.990000000,gmvSellerLcAmt:59.9900000000000000,gmvUsdAmt:59.9900000000000000,instrumentType:null,itemId:4966893,itemPrice:59.99,itemSiteId:100,nonPciAccountId:null,omsExternalRfrncId:17-06848-86625,omsOrderId:170000684886625,orderStsId:0,paidDate:2021-04-04 08:06:27,paymentMethod:[34],",
"acctHolderName:null,auctEndDt:2021-04-22,auctTitle:2 X BELT,bankName:null,buyerId:22491011,byrCntryId:2,cardlastFourDigits:5678,checkoutStatus:2,cityShipAddr:null,ckCompleteDate:2020-05-04 08:06:26,cnvstnIdList:null,countryShipAddr:null,createdTime:2020-05-04 08:06:27,firstTrackingDtlId:17834022,gmvBuyerLcAmt:59.990000000,gmvSellerLcAmt:59.9900000000000000,gmvUsdAmt:59.9900000000000000,instrumentType:null,itemId:4955893,itemPrice:59.99,itemSiteId:101,nonPciAccountId:null,omsExternalRfrncId:17-06848-86625,omsOrderId:170000684886625,orderStsId:0,paidDate:2021-03-04 08:06:27,paymentMethod:[34],");
writeListToFile(data, "C:\\Users\\Atihska\\Downloads\\mydata.csv");
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.