在Python中将数据元素导出到文本文件

2024-09-21 05:52:44 发布

您现在位置:Python中文网/ 问答频道 /正文

如何更改以下代码以获取文本文件中要在excel中打印的数据元素表?在

 x = z = vz = vy = ax = ay = time = 0.0
 y = 50 #Initial Height: 50 meters
 vx = 25 #Initial velocity in the x direction: 25 m/s
 az = -9.8 #Constant acceleration in the z direction: -9.8 m/s^2
 deltaTime = 1

 #Initializes a matrix with 3 columns and 1000 rows for each column: Will hold the corresponding x,y,z coordinate of the particle at time t
 positionMatrix = [[None]*1000 for x in range(3)] 

 posArray = [x, y, z] 
 velArray = [vx, vy, vz] 
 accArray = [ax, ay, az]
 timeArray = [i*deltaTime for i in range(1000)]
 #print(timeArray)

 for j in range (1000): #j is the time increment
     for i in range (3): #i is each component (x, y, z)

         #x = x + vx*time + .5*ax*(time*time); #y = y + vy*time + .5*ay*(time*time); #z = z + vz*time + .5*az*(time*time)
         positionMatrix[i][j] = posArray[i] + velArray[i] * timeArray[j] + 1/2*accArray[i] * timeArray[j] * timeArray[j]
         print(positionMatrix)

Tags: theinfortimerangeaxinitialaz
3条回答

不知道你到底是什么意思,但我猜你想在Excel中打开CSV文件。在这种情况下,使用open()创建一个用于写入的文件,在循环时将()值和逗号写入该文件,然后在程序末尾关闭该文件。在

将数据导入Excel的一个非常简单的方法是将其写入CSV(逗号分隔值)文件。每行代表一行,用逗号分隔单元格。例如:

1,2,4
2,5,6,7
abc,1,hello

将此文件另存为.csv文件,Excel将打开它。如果你想用Excel.xls保存更多的计算,可以用Excel文件。在

如果您想从Python直接保存到Excel文件中,可以尝试http://www.python-excel.org/中的模块。但使用CSV几乎肯定更容易!在

保存文件以便在Excel中打开的最好方法可能是使用内置的CSV模块。它将允许您保存一个文件,该文件可以导入到Excel和其他电子表格应用程序中。在

创建一个CSV编写器并使用writerows方法,传递positionMatrix列表。在

请看一下python文档:13.1. csv — CSV File Reading and Writing

相关问题 更多 >

    热门问题