我的代码如下:
x=0
while (x<3):
print('purple'),
print('yellow'),
print({x})
x=x+1
我希望它将此数据记录到名为&#39; daffodils.csv&#39;的csv文件中。 我如何做到这一点,以便迭代不会写在彼此之上?
例如,如果我运行程序两次,我的csv文件将如下所示:
purple yellow 0
purple yellow 1
purple yellow 2
purple yellow 0
purple yellow 1
purple yellow 2
由于
答案 0 :(得分:1)
只需使用,
with open('test.txt', 'a') as f:
for x in range(3):
s = 'purple yellow {}\n'.format(x)
f.write(s)
或者根据需要使用csv,
import csv
with open('test.csv', 'a') as f:
writer = csv.writer(f, delimiter=' ')
for x in range(3):
writer.writerow(['purple', 'yellow', x])
答案 1 :(得分:-1)
只需使用:
public Date timeUpdate(Date time){
//time would be the date you want
//new Date() creates a new date and within the parentheses is a timestamp allowing
//you to set the time with an addition of time in milliseconds
//This code basically adds 1 second to the time
return new Date(time.getTime()+1000);
//for a day change inside the parentheses to (time.getTime()+(1000*24*60*60);
}
t = open("daffodils.csv", "a+")
x=0
while (x<3):
t.write('purple'),
t.write('yellow'),
t.write({x})
t.write("\n")
x=x+1
t.close()
以附加模式打开文件,如果a
尚不存在,则+
会创建该文件。