问题描述
Excel 表格中有多列数据,其中第一列为能量值,往后数列对应着各能量值是否存在于某特定核中。需要将其挑出,按核素-能量值重新写成 Excel 表格。
实现
1 # -*- coding: utf-8 -*- 2 """ 3 Created on Tue Jun 16 09:37:13 2020 4 5 @author: kurrrr 6 """ 7 8 import xlrd 9 import xlwt 10 11 file = xlrd.open_workbook('./归属_20200616.xlsx') 12 sheet_read = file.sheet_by_name('总投影谱') 13 14 excl_write = xlwt.Workbook(encoding='utf-8') 15 sheet_write = excl_write.add_sheet('归属细节') 16 17 row_nuclear = ({1: '84Rb', 2: '83Rb', 3: '82Rb', 4: '81Rb', 5: '80Rb', 18 6: '79Rb', 7: '78Rb', 8: '83Kr', 9: '82Kr', 10: '81Kr', 19 11: '80Kr', 12: '79Kr', 13: '78Kr', 14: '81Br', 15: '80Br', 20 16: '78As'}) 21 22 energy = sheet_read.col_values(0) 23 24 for row in row_nuclear: 25 nuclear = sheet_read.col_values(row) 26 nuclear_dic = dict(zip(energy, nuclear)) 27 sheet_write.write(0, row-1, row_nuclear[row]) 28 i = 1 29 for key in nuclear_dic: 30 if "√" in nuclear_dic[key]: 31 sheet_write.write(i, row-1, key) 32 i = i + 1 33 34 excl_write.save('./归属细节_20200616.xls')
- 对同一个 Excel 文件进行读写比较麻烦,不建议那样操作。
- 字典的定义与遍历