简体   繁体   English

如何使用Python在Excel工作表(.xlsx)的列中保存浮点变量

[英]How to save float variables in columns of excel sheet (.xlsx) using python

I am a newbie in Python programming language. 我是Python编程语言的新手。 My aim is to save (or write) my output variables ie a, b and c (as in the following code) in the excel file sheet (.xlsx format) I have tried the following python code, for writing my output variables. 我的目标是将输出变量(即a,b和c)保存(或写入)在excel文件表(.xlsx格式)中(我尝试了以下python代码),用于写入输出变量。 I want an output excel file (.xlsx) like this: See the image of the excel file . 我想要这样的输出excel文件(.xlsx): 请参阅excel文件的图像 The code is working fine with the .csv file, but its not working for .xlsx files 该代码可与.csv文件正常工作,但不适用于.xlsx文件

Following I want to achieve: 以下是我要实现的:

  1. I want to save each variable (a,b,c) in different columns of excel sheet 我想将每个变量(a,b,c)保存在Excel工作表的不同列中
  2. I want to give names to each columns in excel sheet as (in each columns a, b, c will be stored, and the three columns name should be Value, Cross-range and A-value respectively) 我想给excel工作表中的每一列命名(因为将在每列中存储a,b,c,三列的名称分别为Value,Cross-range和A-value)

Could anyone please help me on this ? 有人可以帮我吗? Any suggestions or help regarding this ? 有什么建议或帮助吗?

n=10 
for i in range(n-1):
    a[i+1] = a[i]-2
    b=a/10
    c=b**2
    file = open("sample-test.xlsx","w")
    for i in range(len(y)):
    iter = "%.3f,%.3f,%.5f\n"%(a[i],b[i],c[i])
    print (iter)
    file.write(iter)
    file.close()

Excel files are a binary format (technically it is a .zip file), not a simple text format like a .csv file. Excel文件是二进制格式(从技术上来说是.zip文件),而不是像.csv文件那样的简单文本格式。 If you want to write to a .xlsx file, there are a number of packages that have that capability. 如果要写入.xlsx文件,则有许多具有该功能的软件包。

My preference is for xlsxwriter . 我的首选是xlsxwriter It allows you to create a new Excel workbook, write data to it, and add any formatting you want. 它允许您创建一个新的Excel工作簿,向其中写入数据,并添加所需的任何格式。 I am not sure what your for loop was doing, so I modified it so it would work for this example 我不确定您的for循环在做什么,因此我对其进行了修改,使其可以在本示例中使用

import xlsxwriter

wb = xlsxwriter.Workbook('sample-test.xlsx')
ws = wb.add_worksheet('my sheet')

# write the header in row 0, which is Excel row 1
ws.write_row(0, 0, ['chickens', 'ducks', 'mice'])

n = 10
a = 98
for i in range(n-1):
    a = a * i - 2
    b=a/10
    c=b**2
    ws.write_row(i+1, 0, [a,b,c])

wb.close()
from openpyxl import Workbook
wb = Workbook() 
# grab the active worksheet
ws = wb.active 
# Data can be assigned directly to cells         
ws['A1'] = 42.2
# Rows can also be append
ws.append([1, 2, 3])

# Save the file
wb.save("sample.xlsx")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将 python 结果保存为 excel xlsx - How to save python result as excel xlsx 如何使用python将绘图图作为图像保存到excel表中? - how to save a plotly graph to excel sheet as an image using python? 如何从sql数据库导出具有多个工作表的xlsx文件并使用python导出列 - How to export from sql database an xlsx file with multiple sheet and exporting columns using python 如何在python中使用panda在现有excel表中追加列 - how to append columns in existing excel sheet using panda in python 使用 Python 自动调整 Excel 工作表的所有列 - Using Python To Autofit All Columns of an Excel Sheet 如何使用 python 将 Excel SpreadsheetML 转换为.xlsx - How to convert Excel SpreadsheetML to .xlsx using python 使用 Python:过滤 Excel 工作表并将过滤后的视图另存为新工作表 - Using Python: Filter an Excel Sheet and Save Filtered View as New Sheet 如何使用 python 将文本文件拆分为行和列并保存在 excel 中 - How to split the text file into rows and columns and save in excel using python 如何使用Python合并来自不同工作簿的多个同名excel工作表并保存到新的excel工作簿中 - How to merge multiple excel sheet with the same name from different workbooks and save into a new excel workbook using Python 如何使用python将多个xlsx表转换为csv - How to convert multiple xlsx sheet to csv using python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM