首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >数据收集任务

数据收集任务
EN

Stack Overflow用户
提问于 2010-10-21 11:32:35
回答 1查看 24关注 0票数 0

我有遵循这种模式的数据:

代码语言:javascript
复制
ID      Name1     Name2    Name3    Name4    .....
41242   MCJ5X     TUAW     OXVM4    Kcmev 1
93532   AVEV2     WCRB3    LPAQ 2   DVL2
.
.
.

到目前为止,这只是电子表格中的格式,大约有6000行。我需要做的是为Name1之后的每个名称创建一个新行,并将其与当前行上的ID相关联。例如,如下所示:

代码语言:javascript
复制
ID      Name1 
41242   MCJ5X     
41242   TUAW    
41242   OXVM4    
41242   Kcmev 1
93532   AVEV2     
93532   WCRB3    
93532   LPAQ 2   
93532   DVL2

你知道我该怎么做吗?我觉得这不应该太复杂,但不确定最好的方法。无论是脚本还是函数,我都会非常感谢你的帮助。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-10-21 11:47:51

如果可能,您可能希望使用csv文件。这些文件是纯文本的,大多数电子表格程序可以打开/修改它们(我知道Excel和OpenOffice版本可以)。如果你采用这种方法,你的算法将如下所示:

代码语言:javascript
复制
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 (取决于平台)可能是一个很好的选择。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3984084

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档