1. 写文件
对每个元素单独写入:
import xlwt
file = xlwt.Workbook()
table1 = file.add_sheet("table name 1")
table2 = file.add_sheet("table name 2")
table1.write(row, column, value)
table2.write(row, column, value)
file.save("filename.xls")
2. 读文件
import xlrd
import numpy as np
data = xlrd.open_workbook('result.xls')
sheet = data.sheet_names()[0]
sheetname = data.sheet_by_name(sheet)
rows = sheetname.nrows # 行数
cols = sheetname.ncols # 列数
HW = []
for i in range(1, rows):
HW.append(sheetname.cell_value(i, 1))
plaintext = np.zeros([rows, 16], dtype=int)
j = 0
for plain in HW:
for i in range(16):
cell = int(plain[i*2], 16) *16 + int(plain[i*2+1], 16)
plaintext[j, i] = cell
j += 1
print(plaintext)
Excel数据读写Python实现

本文详细介绍使用Python的xlrd和xlwt库进行Excel文件的数据读取与写入操作。包括如何创建工作簿,添加表格,写入数据,以及如何读取Excel文件中的数据,并转换为NumPy数组进行进一步处理。
1417

被折叠的 条评论
为什么被折叠?



