简体   繁体   English

Python:如何以表格形式将数据写入电子表格

[英]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.

相关问题 如何编写python脚本来操纵谷歌电子表格数据 - How to write a python script to manipulate google spreadsheet data 如何使用 Python 中的 Google Sheets API 在电子表格的特定选项卡中写入数据? - How to write data in a specific tab of a spreadsheet with the Google Sheets API in Python? 如何在python中将系统信息写入电子表格 - how to write system info to a spreadsheet in python 如何使用 Python 写入 Excel 电子表格? - How to write to an Excel spreadsheet using Python? 如何在 Python 中使用 xlsxwriter 将德语变音符号写入电子表格 - How to write German umlauts to a spreadsheet with xlsxwriter in Python 如何创建数据表布局等电子表格? - How to create a spreadsheet like layout for data table? 如何使用gspread将表格(列表列表)写入Google Spreadsheet - How to write a table (list of lists) to Google Spreadsheet using gspread 如何编写Python脚本来摄取此JSON数据并将其转换为数据表 - how to write Python script that ingests this JSON data and transforms it into a data table 如何使用 python 中的请求从 google 电子表格表格中获取数据? - How do I get the data from a table of google spreadsheet using requests in python? python从excel电子表格中提取数据到json表单 - python extract data from excel spreadsheet to json form
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM