[英]Python: How to write to data in a form of table to a spreadsheet
I have a code that calculates a given function at different points, that is : 我有一个代码,可以在不同点计算给定的函数,即:
def f(x):
return np.sin(x)
x=np.arange(-1,1,0.01)
print np.sin(x)
I want to store the values to an excel sheet using this format: 我想使用以下格式将值存储到Excel工作表中:
Is there a simple way to do it without using the pandas DataFrame since it this package is now working on my laptop 有没有一种简单的方法可以不使用pandas DataFrame来做到这一点,因为它现在可以在我的笔记本电脑上使用了
An easy way to do it is use the csv
module to write to a csv file, which can then be read into excel. 一种简单的方法是使用csv
模块写入csv文件,然后可以将其读入excel。
An example would be: 一个例子是:
import csv
import numpy as np
def f(val):
return np.sin(val)
x = np.arange(-1, 1, 0.01)
y = f(x)
with open('outfile.csv', 'w') as fid:
writer = csv.writer(fid)
writer.writerow(['x'] + list(x))
writer.writerow(['f(x)'] + list(y))
On the other hand, if you really want to write to excel, you can use packages that handle excel files. 另一方面,如果您确实想写excel,则可以使用处理excel文件的软件包。 You can refer to http://www.python-excel.org/ for a list of relevant packages that are not pandas
您可以访问http://www.python-excel.org/获取非pandas
相关软件包的列表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.