首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >拆分行并写入列

拆分行并写入列
EN

Stack Overflow用户
提问于 2022-03-11 20:09:51
回答 1查看 39关注 0票数 0

我有一个数据文件输出,比如

代码语言:javascript
复制
0     0.010168   7191516  branch-instructions
1     0.010168  33047525         instructions
2     0.010168    290566     cache-references
3     0.010168    104974         cache-misses
4     0.010168  25109349           cpu-cycles
...        ...       ...                  ...
1913  3.376448   2524814         instructions
1914  3.376448     32524     cache-references
1915  3.376448     18957         cache-misses
1916  3.376448   2144874           cpu-cycles
1917  3.376448      3349        branch-misses

我需要将它写到csv中,第三列作为标题,与其对应的值如下

代码语言:javascript
复制
time       branch-instructions  instructions   cache-references   cache-misses  cpu-cycles  branch-misses
0.010168   7191516              33047525       290566             104974        25109349    2458  

我该怎么做?任何帮助都是非常感谢的。

EN

回答 1

Stack Overflow用户

发布于 2022-03-11 20:24:16

您需要在第二列上循环,写出时间,然后只写值,直到到达不同的时间为止。您可能希望同时控制第四列中的文本是正确的。

因此,步骤如下:

  • 循环,在每一行检查时间内,当
  • 第一次在新的csv
  • 中写入值时,如果时间不同,则更改第一次,并在新的csv

h 19再次迭代H 210F 211中创建一条新行。

与…有关的东西:

代码语言:javascript
复制
#Prepare reepting data
toReceive = ["time", "branch-instructions", "instructions"]#....
#Split by line your file
lines = data_imported.split("\n")
sameTime = True
i=0
refTime=lines[i].split(" ")[1]
#Loop with time as criteria
while(i<len(lines)):
  sameTIme=True
  localReceive=refTime
  while(sameTime):
     #Split by elements
     elements = lines[i].split(" ")
     sameTime=elements[1]==refTime
     localReceive.append(elements[2])
  refTime=elements[1]
  toReceive.append(localReceive)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71444142

复制
相关文章

相似问题

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