使用'for'循环在Python中写入CSV文件(Writing to a CSV file in Python using a 'for' loop)

编程入门 行业动态 更新时间:2024-10-27 06:23:44
使用'for'循环在Python中写入CSV文件(Writing to a CSV file in Python using a 'for' loop)

我目前正在尝试用python写入CSV文件,但我只想每60次迭代打印一次'CurrentInv'。 例如

outfile = open("Pension.csv", 'w') for k in range(1,Iterations+1): outfile.write(str( str(k) + ',')) outfile.write(str(CurrentInv[k][0])+',') outfile.write(str(CurrentInv[k][60])+',') outfile.write(str(CurrentInv[k][120])+',') outfile.write(str(CurrentInv[k][180])+',') outfile.write(str(CurrentInv[k][240])+',') outfile.write(str(CurrentInv[k][300])+',') outfile.write(str(CurrentInv[k][360])+',') outfile.write(str('\n')) outfile.close()

但我想循环获得这个。 我努力了

outfile = open("Pension5.csv", 'w') for k in range(1,Iterations+1): outfile.write(str( str(k) + ',')) for i in range(0,Months+1): outfile.write(str(CurrentInv[k][i])+',') i=i+60 outfile.write(str('\n')) outfile.close()`

但是,这仍然会将CurrentInv的所有值从0打印到几个月而不是每60次迭代。 任何帮助,将不胜感激!

I'm currently trying to write to a CSV file in python, however I only want to print 'CurrentInv' every 60 iterations. For example

outfile = open("Pension.csv", 'w') for k in range(1,Iterations+1): outfile.write(str( str(k) + ',')) outfile.write(str(CurrentInv[k][0])+',') outfile.write(str(CurrentInv[k][60])+',') outfile.write(str(CurrentInv[k][120])+',') outfile.write(str(CurrentInv[k][180])+',') outfile.write(str(CurrentInv[k][240])+',') outfile.write(str(CurrentInv[k][300])+',') outfile.write(str(CurrentInv[k][360])+',') outfile.write(str('\n')) outfile.close()

But I would like to obtain this in a loop. I have tried

outfile = open("Pension5.csv", 'w') for k in range(1,Iterations+1): outfile.write(str( str(k) + ',')) for i in range(0,Months+1): outfile.write(str(CurrentInv[k][i])+',') i=i+60 outfile.write(str('\n')) outfile.close()`

However, this still prints all values for CurrentInv from 0 to Months instead of every 60 iterations. Any help would be appreciated!

最满意答案

请尝试以下方法:

with open("Pension5.csv", 'w') as outfile: for k in range(1, Iterations+1): outfile.write(str(str(k) + ',')) for i in range(0, Months+1, 60): outfile.write(str(CurrentInv[k][i]) + ',') outfile.write(str('\n'))

它为该范围指定了60的步长,以便每次迭代它添加60而不是1.在示例情况下,月份应为360。 如果您希望Months为6而不是360,请检查以下内容:

with open("Pension5.csv", 'w') as outfile: for k in range(1, Iterations+1): outfile.write(str(str(k) + ',')) for i in range(0, Months+1): outfile.write(str(CurrentInv[k][i*60]) + ',') outfile.write(str('\n'))

Try the following:

with open("Pension5.csv", 'w') as outfile: for k in range(1, Iterations+1): outfile.write(str(str(k) + ',')) for i in range(0, Months+1, 60): outfile.write(str(CurrentInv[k][i]) + ',') outfile.write(str('\n'))

It specifies a step of 60 for the range so that each iteration it adds 60 instead of 1. Months should be 360 in the example case. If you want Months to be 6 instead of 360 check the following:

with open("Pension5.csv", 'w') as outfile: for k in range(1, Iterations+1): outfile.write(str(str(k) + ',')) for i in range(0, Months+1): outfile.write(str(CurrentInv[k][i*60]) + ',') outfile.write(str('\n'))

更多推荐

本文发布于:2023-08-07 15:04:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464763.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   CSV   Python   loop   file

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!