Convert an excel or spreadsheet column letter to its number in Pythonic fashion

前端 未结 17 1191
夕颜
夕颜 2020-12-09 04:18

Is there a more pythonic way of converting excel-style columns to numbers (starting with 1)?

Working code up to two letters:



        
17条回答
  •  囚心锁ツ
    2020-12-09 04:57

    For index that starts from zero (e.g. A = 0, B = 1, and so on):

    def col_to_index(col):
        A = ord('A')
        return sum(i * 26 + (ord(c) - A) for i, c in enumerate(col[::-1].upper()))
    

提交回复
热议问题