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

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

    One-liners tested in Python 2.7.1 and 3.5.2

    excel_col_num = lambda a: 0 if a == '' else 1 + ord(a[-1]) - ord('A') + 26 * excel_col_num(a[:-1])
    
    excel_col_name = lambda n: '' if n <= 0 else excel_col_name((n - 1) // 26) + chr((n - 1) % 26 + ord('A'))
    

    Multi-liners likewise

    def excel_column_name(n):
        """Number to Excel-style column name, e.g., 1 = A, 26 = Z, 27 = AA, 703 = AAA."""
        name = ''
        while n > 0:
            n, r = divmod (n - 1, 26)
            name = chr(r + ord('A')) + name
        return name
    
    def excel_column_number(name):
        """Excel-style column name to number, e.g., A = 1, Z = 26, AA = 27, AAA = 703."""
        n = 0
        for c in name:
            n = n * 26 + 1 + ord(c) - ord('A')
        return n
    
    def test (name, number):
        for n in [0, 1, 2, 3, 24, 25, 26, 27, 702, 703, 704, 2708874, 1110829947]:
            a = name(n)
            n2 = number(a)
            a2 = name(n2)
            print ("%10d  %-9s  %s" % (n, a, "ok" if a == a2 and n == n2 else "error %d %s" % (n2, a2)))
    
    test (excel_column_name, excel_column_number)
    test (excel_col_name, excel_col_num)
    

    All tests print

             0             ok
             1  A          ok
             2  B          ok
             3  C          ok
            24  X          ok
            25  Y          ok
            26  Z          ok
            27  AA         ok
           702  ZZ         ok
           703  AAA        ok
           704  AAB        ok
       2708874  EXCEL      ok
    1110829947  COLUMNS    ok
    

提交回复
热议问题