zoukankan      html  css  js  c++  java
  • python实现Excel的表头与索引之间的转换

    字母转数字

    def get_index(capital):
        """
        大写字母(Excel列头)转索引
        :param capital: 'A' --> 0, 'AA' --> 26
        :return: int
        """
        number = 0
        capital = capital.upper()
        for char in capital:
            number = number * 26 + ord(char) - ord('A') + 1
        return number - 1
    

    数字转字母

    def get_char(number):
        """
        索引转大写字母(Excel列头)
        :param number: 0 --> 'A', 26 --> 'AA'
        :return: str
        """
        factor, moder = divmod(number, 26)
        mod_char = chr(moder + 65)
        if factor:
            mod_char = get_char(factor - 1) + mod_char
        return mod_char
    
    博主箴言:该博文中的文字内容仅供参考学习,如有不当之处还望各位不吝赐教,为博主指点一二,不胜感激:▄︻┻┳═一…… ☆(>○<)
  • 相关阅读:
    反射 元类
    多态
    封装
    继承
    面向基础
    包 logging模块 hashlib模块 openpyxl 深浅拷贝
    常用模块
    re模块(正则表达式)
    模块 导入方式 软件开发目录规范
    第 3 章 镜像
  • 原文地址:https://www.cnblogs.com/rongge95500/p/15222200.html
Copyright © 2011-2022 走看看