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

前端 未结 17 1223
夕颜
夕颜 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:51

    I'm not sure I understand properly, do you want to "translate" the referenced C# code to python? If so, you were on the right track; just modify it so:

    def column_to_number(c):
      """Return number corresponding to excel-style column."""
      sum = 0
      for l in c:
        if not l in string.ascii_letters:
          return False
        sum*=26
        sum+=ord(l.upper())-64
      return sum
    

提交回复
热议问题