[Python] execl读写
2024-10-19 14:54:07
相关库
读:xlrd
写:xlwt
案例
要求:
将图1中的数据导以图2的形式写入另一个文件中
第一列索引关系:{1:K1-B1,2:K1-B2}
。。。(18列)
思路:
按行读取数据,根据索引关系按列写入数据
代码:
1 import xlrd
2 import xlwt
3 #柱号映射关系
4 index = ['K1-B1','K1-B2']
5 load_cnt = 6
6 #读excel文件
7 wb = xlrd.open_workbook(r'C:\Users\Administrator\Desktop\demo\111.xls')
8 sheet1 = wb.sheet_by_index(0)
9 index_col = sheet1.col_values(0)[1:]
10 joint_cnt = len(index_col)
11 #写excel文件
12 f = xlwt.Workbook()
13 sheet2 = f.add_sheet(u'sheet1',cell_overwrite_ok=True)
14 for i in range(joint_cnt):
15 for j in range(joint_cnt):
16 if index_col[j] == index[i]: break
17 target_row = sheet1.row_values(j+1)[1:]
18 num_cnt = len(target_row)
19 for k in range(load_cnt):
20 sheet2.write(3*i,k,target_row[3*k])
21 sheet2.write(3*i+1,k,target_row[3*k+1])
22 sheet2.write(3*i+2,k,target_row[3*k+2])
23 f.save(r'C:\Users\Administrator\Desktop\demo\222.xls')
参考
xlrd帮助
https://blog.csdn.net/Sukie_csdn/article/details/78672626
openpyxl帮助
https://segmentfault.com/a/1190000016256490
常用操作实现
https://blog.csdn.net/paul0127/article/details/82529153
最新文章
- Android四大组件-Activity
- update
- tensorflow1
- PHP __DIR__, __FILE__, __FUNCTION__, __CLASS__, __METHOD__, __LINE__, __NAMESPACE__
- 16.(转) Android之Support v4、v7、v13的区别和应用场景
- java 打包过程及如何使用第三方jar包
- 设置页面不缓存 no-cache
- [Android]使用platform密钥来给apk文件签名的命令
- #1042 - Can't get hostname for your address
- 14.18.1 The InnoDB Recovery Process InnoDB 恢复进程:
- 学习Easyui
- 使用Java实现二叉树的添加,删除,获取以及遍历
- windows server 2016 x64用MecaCli工具检查raid5磁盘状态
- 天气预测(CNN)
- How to change Eclipse loading image
- android checkBox选中与取消
- SoapUI SoapUI测试WebService协议接口简介
- 【转】苹果App Store审核指南中文翻译(更新)
- c++并发编程之互斥锁(mutex)的使用方法
- HA 高可用mysql集群