zoukankan      html  css  js  c++  java
  • python基础 字典

    问题描述


    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')
    View Code
    • 对同一个 Excel 文件进行读写比较麻烦,不建议那样操作。
    • 字典的定义与遍历

     

  • 相关阅读:
    共享内存
    文件IO函数和标准IO库的区别
    链表程序
    flash_header.S ( freescale imx6 board)
    深入理解二维数组
    putchar和puts
    指针目标
    C语言:break和continue
    C语言:输入输出
    python lambda
  • 原文地址:https://www.cnblogs.com/kurrrr/p/13150081.html
Copyright © 2011-2022 走看看