用groovy修改csv
新手需要Groovy的帮助。我想修改下一个.csv文件:用groovy修改csv,groovy,Groovy,新手需要Groovy的帮助。我想修改下一个.csv文件: Agency Name:IKEA,,,,,,,,,,,,,, Advertiser Name: Ingka,,,,,,,,,,,,,, Campaign Name:All,,,,,,,,,,,,,, Date Resolution:Days,,,,,,,,,,,,,, Campaign Dates:N/A,,,,,,,,,,,,,, Report Date Range:Last X Days (25.06.2020 - 01.07.202
Agency Name:IKEA,,,,,,,,,,,,,,
Advertiser Name: Ingka,,,,,,,,,,,,,,
Campaign Name:All,,,,,,,,,,,,,,
Date Resolution:Days,,,,,,,,,,,,,,
Campaign Dates:N/A,,,,,,,,,,,,,,
Report Date Range:Last X Days (25.06.2020 - 01.07.2020),,,,,,,,,,,,,,
Report Creation Date: 02.07.2020 5:26:18 (GMT -5 Eastern Standard Time),,,,,,,,,,,,,,
You must save the report locally to create a pivot table based on the report data.,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,
Advertiser Name,Advertiser ID,Campaign Name,Campaign ID,Date,Site Name,Site ID,Device Type,Placement Name,Placement ID,Clickthrough URL,* Clicks,* Served Impressions,* Total Recordable Impressions (IAB),* Total Viewable Impressions (IAB)
Ingka,190530,1_flight_0119,947535,25.06.2020,Auditorius SE,101304,Smart Phone,Flight_EK_Auditorius_Video_mobile,27353235,https://www.ikea.com/promo/wifi?utm_source=Auditorius&utm_medium=Video_mobile,0,0,0,0
Ingka,190530,1_flight_0119,947535,28.06.2020,Between Exchange SE,124598,PC,Flight_IQP_Between_Exchange_Banner_728x90_DCO,27359134,,0,0,0,0
Data was updated last on 7/2/2020 12:00:00 AM (GMT -5 Eastern Standard Time),,,,,,,,,,,,,,
Viewability mode is set per individual campaign,,,,,,,,,,,,,,
我想要的是:
广告商名称、广告商ID、活动名称、活动ID、报告日期、站点、站点ID、设备、PlacementName、PlacementID、URL、点击、印象、TotalRecordableImpressions、TotalViewableImpressions
在阅读了一些有关在groovy中解析CSV的主题后,我尝试了以下方法:
def reader = new StringReader('''...''') //or FileReader/InputStream/whatever
def mapList = []
reader.splitEachLine(','){ parts ->
mapList << [
AdvertiserName:parts[0],
AdvertiserID:parts[1],
CampaignName:parts[2],
CampaignID:parts[3],
ReportDate:parts[4],
Site:parts[5],
SiteID:parts[6],
Device:parts[7],
PlacementName:parts[8],
PlacementID:parts[9],
URL:parts[10],
Clicks:parts[11],
Impressions:parts[12],
TotalRecordableImpressions:parts[13],
TotalViewableImpressions:parts[14]
]
}
def result = mapList.findAll{ it.value!=null }
因为这是一个有地图的列表。我试图用以下方法修复它:
mapList.collectEntries() {
[it.AdvertiserName, it.AdvertiserID, ...]
}
但它也会让人觉得奇怪。直截了当:
def str ='''\
Agency Name:IKEA,,,,,,,,,,,,,,
Advertiser Name: Ingka,,,,,,,,,,,,,,
Campaign Name:All,,,,,,,,,,,,,,
Date Resolution:Days,,,,,,,,,,,,,,
Campaign Dates:N/A,,,,,,,,,,,,,,
Report Date Range:Last X Days (25.06.2020 - 01.07.2020),,,,,,,,,,,,,,
Report Creation Date: 02.07.2020 5:26:18 (GMT -5 Eastern Standard Time),,,,,,,,,,,,,,
You must save the report locally to create a pivot table based on the report data.,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,
Advertiser Name,Advertiser ID,Campaign Name,Campaign ID,Date,Site Name,Site ID,Device Type,Placement Name,Placement ID,Clickthrough URL,* Clicks,* Served Impressions,* Total Recordable Impressions (IAB),* Total Viewable Impressions (IAB)
Ingka,190530,1_flight_0119,947535,25.06.2020,Auditorius SE,101304,Smart Phone,Flight_EK_Auditorius_Video_mobile,27353235,https://www.ikea.com/promo/wifi?utm_source=Auditorius&utm_medium=Video_mobile,0,0,0,0
Ingka,190530,1_flight_0119,947535,28.06.2020,Between Exchange SE,124598,PC,Flight_IQP_Between_Exchange_Banner_728x90_DCO,27359134,,0,0,0,0
Data was updated last on 7/2/2020 12:00:00 AM (GMT -5 Eastern Standard Time),,,,,,,,,,,,,,
Viewability mode is set per individual campaign,,,,,,,,,,,,,,
'''
def headers = [ 'AdvertiserName', 'AdvertiserID', 'CampaignName', 'CampaignID', 'ReportDate', 'Site',
'SiteID', 'Device', 'PlacementName', 'PlacementID', 'URL', 'Clicks', 'Impressions',
'TotalRecordableImpressions', 'TotalViewableImpressions' ].withIndex()
def list = []
boolean start
str.splitEachLine(','){ parts ->
if( 'Advertiser Name' == parts[ 0 ] ){
start = true
return
}else if( !start || 15 != parts.size() ) return
list << headers.collectEntries{ [ it[ 0 ], parts[ it[ 1 ] ] ] }
}
println list.join( '\n' )
要将数据放回文件中,您需要smth,如:
def file = new StringWriter() // replace with new File( '../output.csv' )
file.withWriter{ out ->
out << headers*.first().join( ',' )
list.each{
out << '\n' << it.values().join( ',' )
}
}
file
伟大的但是如何将这些地图打包回类似.csv的格式呢?如我所见,此列表中的每个映射=.csv中的每一行,这些映射中的每个键都是所有行的公共标题。请参阅更新。。。
mapList.collectEntries() {
[it.AdvertiserName, it.AdvertiserID, ...]
}
def str ='''\
Agency Name:IKEA,,,,,,,,,,,,,,
Advertiser Name: Ingka,,,,,,,,,,,,,,
Campaign Name:All,,,,,,,,,,,,,,
Date Resolution:Days,,,,,,,,,,,,,,
Campaign Dates:N/A,,,,,,,,,,,,,,
Report Date Range:Last X Days (25.06.2020 - 01.07.2020),,,,,,,,,,,,,,
Report Creation Date: 02.07.2020 5:26:18 (GMT -5 Eastern Standard Time),,,,,,,,,,,,,,
You must save the report locally to create a pivot table based on the report data.,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,
Advertiser Name,Advertiser ID,Campaign Name,Campaign ID,Date,Site Name,Site ID,Device Type,Placement Name,Placement ID,Clickthrough URL,* Clicks,* Served Impressions,* Total Recordable Impressions (IAB),* Total Viewable Impressions (IAB)
Ingka,190530,1_flight_0119,947535,25.06.2020,Auditorius SE,101304,Smart Phone,Flight_EK_Auditorius_Video_mobile,27353235,https://www.ikea.com/promo/wifi?utm_source=Auditorius&utm_medium=Video_mobile,0,0,0,0
Ingka,190530,1_flight_0119,947535,28.06.2020,Between Exchange SE,124598,PC,Flight_IQP_Between_Exchange_Banner_728x90_DCO,27359134,,0,0,0,0
Data was updated last on 7/2/2020 12:00:00 AM (GMT -5 Eastern Standard Time),,,,,,,,,,,,,,
Viewability mode is set per individual campaign,,,,,,,,,,,,,,
'''
def headers = [ 'AdvertiserName', 'AdvertiserID', 'CampaignName', 'CampaignID', 'ReportDate', 'Site',
'SiteID', 'Device', 'PlacementName', 'PlacementID', 'URL', 'Clicks', 'Impressions',
'TotalRecordableImpressions', 'TotalViewableImpressions' ].withIndex()
def list = []
boolean start
str.splitEachLine(','){ parts ->
if( 'Advertiser Name' == parts[ 0 ] ){
start = true
return
}else if( !start || 15 != parts.size() ) return
list << headers.collectEntries{ [ it[ 0 ], parts[ it[ 1 ] ] ] }
}
println list.join( '\n' )
[AdvertiserName:Ingka, AdvertiserID:190530, CampaignName:1_flight_0119, CampaignID:947535, ReportDate:25.06.2020, Site:Auditorius SE, SiteID:101304, Device:Smart Phone, PlacementName:Flight_EK_Auditorius_Video_mobile, PlacementID:27353235, URL:https://www.ikea.com/promo/wifi?utm_source=Auditorius&utm_medium=Video_mobile, Clicks:0, Impressions:0, TotalRecordableImpressions:0, TotalViewableImpressions:0]
[AdvertiserName:Ingka, AdvertiserID:190530, CampaignName:1_flight_0119, CampaignID:947535, ReportDate:28.06.2020, Site:Between Exchange SE, SiteID:124598, Device:PC, PlacementName:Flight_IQP_Between_Exchange_Banner_728x90_DCO, PlacementID:27359134, URL:, Clicks:0, Impressions:0, TotalRecordableImpressions:0, TotalViewableImpressions:0]
def file = new StringWriter() // replace with new File( '../output.csv' )
file.withWriter{ out ->
out << headers*.first().join( ',' )
list.each{
out << '\n' << it.values().join( ',' )
}
}
file
AdvertiserName,AdvertiserID,CampaignName,CampaignID,ReportDate,Site,SiteID,Device,PlacementName,PlacementID,URL,Clicks,Impressions,TotalRecordableImpressions,TotalViewableImpressions
Ingka,190530,1_flight_0119,947535,25.06.2020,Auditorius SE,101304,Smart Phone,Flight_EK_Auditorius_Video_mobile,27353235,https://www.ikea.com/promo/wifi?utm_source=Auditorius&utm_medium=Video_mobile,0,0,0,0
Ingka,190530,1_flight_0119,947535,28.06.2020,Between Exchange SE,124598,PC,Flight_IQP_Between_Exchange_Banner_728x90_DCO,27359134,,0,0,0,0