Python有沒有類似Matlab中xlswrite這種寫入數據到Excel的方式?
類似
xlswrite(filename,kridetrack,"result","D11");這種
Python和office交互的第三方庫都比較難用而且文檔不好。最推薦使用萬能的中轉pandas,因為pandas的IO工具非常多,詳情見pandas官方文檔的IO部分。
PS:如果你是為了在Excel中做後續分析,可以考慮直接用csv格式。如果是為了用在其他軟體或者工具裡面,csv可能是更友好的方式,更容易導入各種各種的工具裡面。當然如果是要求,那隻能轉Excel。
PS的PS:用Python解決問題,更快的方式常常是直接繞過去,Python完整強大的工具鏈會給你很多其他路走。
謝邀,我一般來說使用pandas(極力推薦)
import pandas as pd
data = pd.DataFrame()
data["label_1"] = [1, 2, 3, 4, 5]
data["label_2"] = ["a", "b", "c", "d", "e"]
data["label_3"] = ["y", 23, "h", 42, "f"]
data.to_excel(save_path, index=False)
如果你只是要寫入數據的話推薦使用XlsxWriter這個模塊。安裝:
pip install xlsxwriter
項目地址:
Creating Excel files with Python and XlsxWriterjmcnamara/XlsxWriter模塊是用python寫的,不依賴其他第三方的包。支持單元格樣式設置、條件格式、圖表、公式、vba、Pandas等:
我自己有幾個簡單的數據處理項目裡面用到了這個模塊,速度非常快,缺點是不能指定格式後再寫入,必須要在寫的時候制定好格式,格式可以用dict指定(下面是我自己的項目中的例子)。格式聲明: title_format_dict = {"font_name": "Consolas", "font_size": 18,
"bold": True,"align": "center", "valign": "vcenter"}
info_format_dict = {"font_name": "Consolas", "font_size": 12,
"align": "center", "valign": "vcenter"}
header_format_dict = {"font_name": "Consolas", "font_size": 12, "bg_color": "#D9D9D9",
"align": "center", "valign": "vcenter", "border":1}
data_format_dict = {"font_name": "Consolas", "font_size": 12,
"align": "center", "valign": "vcenter", "border":1}
title_format = workbook.add_format(title_format_dict)
info_format = workbook.add_format(info_format_dict)
header_forma = workbook.add_format(header_format_dict)
data_format = workbook.add_format(data_format_dict)
diag_format = workbook.add_format({"diag_type": 1})
warning_format = workbook.add_format({"bg_color": "yellow"})
# Write title, plant informarion, headers
worksheet.merge_range("A2:H2", "XXXXXXXXX", title_format)
worksheet.write_row("B3", ["Power plant:", test_info["site"],
"unit:", test_info["unit"]], info_format)
worksheet.merge_range("A5:A7", "Step", header_forma)
worksheet.merge_range("B5:C5", "Real Step", header_forma)
worksheet.merge_range("D5:G5", "Max Error", header_forma)
worksheet.merge_range("H5:H6", "Hysteresis", header_forma)
worksheet.write_row("B6", ["Withdraw", "Insert", "Withdraw(-)", "Withdraw(+)",
"Insert(-)", "Insert(+)"], header_forma)
worksheet.write_row("B7", ["Sw", "Si", "≥-4", "≤+7", "≥-4", "≤+7", "≤1",], header_forma)
Tutorial 1: Create a simple XLSX file
import xlsxwriter
# Create a workbook and add a worksheet.
workbook = xlsxwriter.Workbook("Expenses01.xlsx")
worksheet = workbook.add_worksheet()
# Some data we want to write to the worksheet.
expenses = (
["Rent", 1000],
["Gas", 100],
["Food", 300],
["Gym", 50],
)
# Start from the first cell. Rows and columns are zero indexed.
row = 0
col = 0
# Iterate over the data and write it out row by row.
for item, cost in (expenses):
worksheet.write(row, col, item)
worksheet.write(row, col + 1, cost)
row += 1
# Write a total using a formula.
worksheet.write(row, 0, "Total")
worksheet.write(row, 1, "=SUM(B1:B4)")
workbook.close()
如果只是寫數據,xlwt足矣,沒必要搞那麼複雜如果要操作07格式或者需要讀寫聯動處理,個人建議還是openpyxl(開發文檔:openpyxl - A Python library to read/write Excel 2010 xlsx/xlsm files - openpyxl 2.4.1 documentation),一個簡簡單單的庫就能實現Excel幾乎所有常見操作。
推薦用pandas中轉,如果簡單單元修改用xlwt or openpyxl
pandas基本上能滿足了吧
可以用openpyxls這個庫
中轉站Pandas隨意轉關於Excel的標準庫也有
要求不高的話import csv就可以。答非所問,求摺疊。
python不存在有沒有,只存在找沒找
1. 這種東西谷歌一下一大堆庫2. 純寫入(創建)的話,最好用模板引擎構造 Excel 的庫,更加方便。
這種東西 真的就不會 google 一下么...
xlwings需要Pywin32
推薦閱讀:
※機械的學生,MATLAB 應該學些什麼?
※四旋翼可不可以用地面站(PC機)來實現姿態結算和控制,機載處理器只做讀數據和收發數據用?
※如何評價 MATLAB R2017a (Prerelease)?
※matlab怎麼把圓周率按照這種方式環形排列?
※請問Matlab如何計算大整數?比如 1000! ?
TAG:Python | MATLAB | MicrosoftExcel |