简体   繁体   English

Python:如何通过循环逐行编写 xls 文件?

[英]Python: How do I write a xls file line-by-line through a loop?

I currently have a for-loop running about 3000 iterations, and at the end of the loop, the values I want are in EGr:我目前有一个运行大约 3000 次迭代的 for 循环,在循环结束时,我想要的值在 EGr 中:

EG = la.eigvals(H)
EGr = EG.real

And EGr is a 1 x 8 array. EGr 是一个 1 x 8 的数组。 Example:例子:

[ 0.22478205  2.50936963  1.81160702  1.76320129  1.94243736  1.81346264
  1.94243736  1.81346264]

How do I write these 8 values in a text/excel file before the loop proceeds on to the next iteration?在循环进行下一次迭代之前,如何在文本/excel 文件中写入这 8 个值?

At the end of the loop, I wish to have only 1 file with all the values of the loop in it.在循环结束时,我希望只有 1 个文件,其中包含循环的所有值。

Here's a pretty basic approach using csv :这是使用csv的一个非常基本的方法:

with open("out.csv", "w") as f:
  writer = csv.writer(f) #Uses Excel dialect by default
  for thing in things:
    #...
    EG = la.eigvals(H)
    EGr = EG.real
    writer.writerow(EGr)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM