使用列"分隔符"写入data.text文件。

时间:2017-04-13 08:09:06

标签: python

我想用适当的格式在Python中编写一个包含数据的file.txt。看下面的代码(它只是我真实代码的一个例子,在数字部分更复杂)

import numpy as np


list_values_r = [0.1,1.0]

points = 10
final_time = 10.0
steep = final_time/points 
time_list = np.arange(0.0,final_time,steep) #list of the time variable


f1 = open("data_file.txt", "w")


for i in list_values_r:
    count = 0
    while (count<points):
        t = time_list[count]
        f1.write(str(t) + " "+str(t**2-5*i)+"\n")
        count +=1



f1.close()

使用这个我获得以下数据集

0.0 -0.5
1.0 0.5
2.0 3.5
3.0 8.5
4.0 15.5
5.0 24.5
6.0 35.5
7.0 48.5
8.0 63.5
9.0 80.5
0.0 -5.0
1.0 -4.0
2.0 -1.0
3.0 4.0
4.0 11.0
5.0 20.0
6.0 31.0
7.0 44.0
8.0 59.0
9.0 76.0

但我想要获得的是四列数据(循环中每次迭代2列),所以:

0.0 -0.5 0.0 -0.5
1.0 0.5 1.0 -4.0
2.0 3.5 2.0 -1.0
3.0 8.5 3.0 4.0
4.0 15.5 4.0 11.0
5.0 24.5 5.0 20.0
6.0 35.5 6.0 31.0
7.0 48.5 7.0 44.0
8.0 63.5 8.0 59.0
9.0 80.5 9.0 76.0

重点是,可能必须存在一些分隔符,如&#34; \ n&#34;为了开始在另一列中写,而不是在下一行,有人知道怎么做呢?感谢。

0 个答案:

没有答案