2010-10-21 43 views
0

我有數據,遵循這種彭定康:數據收集任務

ID  Name1  Name2 Name3 Name4 ..... 
41242 MCJ5X  TUAW  OXVM4 Kcmev 1 
93532 AVEV2  WCRB3 LPAQ 2 DVL2 
. 
. 
. 

截至目前,這是一個電子表格只是格式,並具有約6000行。我需要做的是在Name1之後爲每個Name創建一個新行,並將其與當前行的ID相關聯。例如,見下:

ID  Name1 
41242 MCJ5X  
41242 TUAW  
41242 OXVM4  
41242 Kcmev 1 
93532 AVEV2  
93532 WCRB3  
93532 LPAQ 2 
93532 DVL2 

任何想法,我可以做到這一點?我覺得這不應該太複雜,但不能確定最好的方法。無論是劇本還是某個功能,我都會非常感謝幫助。

+0

任何語言特別?任何文字處理lang都可以。你用awk嗎? – 2010-10-21 04:11:15

回答

0

如果可能,您可能需要使用csv文件。這些文件是純文本的,大多數電子表格程序可以打開/修改它們(我知道Excel和OpenOffice版本可以)。如果你採用這種方法,你的算法看起來像這樣:

read everything into a string array 
create a 1 to many data structure (maybe a Dictionary<string, List<string>> or list of (string, string) tuple types) 

loop over each line of the file 
splice the current line on the ','s and loop over those 
if this is the first splice, add a new item to the 1 to many data structure with the current splice as the Id 
otherwise, add this splice to the "many" (name) part of the last item in the data structure 

create a new csv file or open the old one for writing 
output the "ID, Name1" row 
loop over each 1-many item in the data collection 
loop over the many items in the current 1-many item 
output the 1 (id) + "," + current many item (current name) 

你可以用任何語言來做到這一點。如果它是一次性使用腳本,那麼Python,Ruby或Powershell(取決於平臺)可能是一個不錯的選擇。