简体   繁体   中英

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:

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

And EGr is a 1 x 8 array. 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?

At the end of the loop, I wish to have only 1 file with all the values of the loop in it.

Here's a pretty basic approach using 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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