Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 使用python将while循环的数组输出转换为文本文件_Arrays_File_Text - Fatal编程技术网

Arrays 使用python将while循环的数组输出转换为文本文件

Arrays 使用python将while循环的数组输出转换为文本文件,arrays,file,text,Arrays,File,Text,我正在生成一个温度数组(T)和另一个位置数组(X),我可以使用简单的命令plt.plot(X,T)来绘制它。然而,我正在努力将这些数据输出到文本文件中。 请问,有人能在这方面帮助我吗?非常感谢。透镜(T)打印12,透镜(X)也打印12。 这就是我在Python中尝试的方式: 这些是while循环后的命令,因此数据已经生成。 我想写T[I]和X[I] with open('Temp.txt', 'w') as fh: # this is where I like to write my Tempe

我正在生成一个温度数组(T)和另一个位置数组(X),我可以使用简单的命令plt.plot(X,T)来绘制它。然而,我正在努力将这些数据输出到文本文件中。 请问,有人能在这方面帮助我吗?非常感谢。透镜(T)打印12,透镜(X)也打印12。 这就是我在Python中尝试的方式: 这些是while循环后的命令,因此数据已经生成。 我想写T[I]和X[I]

with open('Temp.txt', 'w') as fh: # this is where I like to write my Temperature data
    for i in range (12): # trying to loop around all the points, up to 12
        fh.write(T[i]) # trying to write temperature to the file, and if it works, need to write the same for the position, X. 

好的,也许你应该试试这个: 当然,我认为您希望两个值都位于同一个文件中,如“T[I]:X[I]”

如果希望每个文件都包含在seprate文件中,则应执行以下操作:

outfile1 = open('outfile1.txt', 'w') 
for a in T:
   outfile1.write(str(a) '\n') 
outfile1.close()
outfile2 = open('outfile2.txt', 'w') 
for a in X:
   outfile2.write(str(a) '\n') 
outfile2.close() 

也许有帮助你可以将数组中的所有成员写入文件谢谢你的消息,但这并没有真正起到帮助作用。太好了,它现在起作用了。您给出的示例是tuple,我得到的数据是list。刚把括号换成了方括号,一切都对我有用。我从来没有遇到过zip(),我将阅读更多关于它的内容。我只有几个月的Python年龄,感谢您分享知识并解决问题。
outfile1 = open('outfile1.txt', 'w') 
for a in T:
   outfile1.write(str(a) '\n') 
outfile1.close()
outfile2 = open('outfile2.txt', 'w') 
for a in X:
   outfile2.write(str(a) '\n') 
outfile2.close()