打开cmd 转入Excel所在文件夹下再打开python,步骤如下:
F:进入F盘
cd 打开F盘下子目录
df = pd.read_excel("data.xlsx")#读取Excel
res = df.to_dict(orient = "record")#输出字典
数组:存放单一数据类型,有限个数据的变量数组操作
列表:存储其值可变的表,列表中的元素类型可不同。
a = [1,2,3]
b = [[1,2],[1,2,3,4],[a,b]]
datas = [{‘name’:‘hmj’,‘sex’:‘1’},{‘name’:‘wmr’,‘sex’:‘0’}]
a = [(1,2),(3,4)]
元组:用于存储值固定不变的值表
c = (1,2,3)
if __name__ == '__main__':
writeDataToExcelFile(datas,'./data.xlsx')
另外还有另一种可以用循环的方法来进行读取和输入
from openpyxl import Workbook
Lable = ['A','B','C','D','E','F','H']
'''
inputData: 列表,含有多个字典;例如:[{'key_a':'123'},{'key_b':'456'}]
outPutFile:输出文件名,例如:'data.xlsx'
'''
def writeDataToExcleFile(inputData,outPutFile):
wb = Workbook()
sheet = wb.active
sheet.title = "Sheet1"
item_0 = inputData[0]
i = 0
for key in item_0.keys():
sheet[Lable[i]+str(1)].value = key
i = i+1
j = 1
for item in inputData:
k = 0
for key in item:
sheet[Lable[k]+str(j+1)].value = item[key]
k = k+1
j = j+1
wb.save(outPutFile)
print('数据写入完毕!')
'''
测试
'''
def test():
datas = [{'name':'hmj','sex':'1'},{'name':'wmr','sex':'0'}]
writeDataToExcleFile(datas,'data.xlsx')
# 执行测试
test()