Convert from numbers to letters

Bill Mill bill.mill at gmail.com
Thu May 19 15:19:46 EDT 2005


On 19 May 2005 11:59:00 -0700, rh0dium <sklass at pointcircle.com> wrote:
> This is great but backwards...
> 
> Ok because you all want to know why..  I need to convert Excel columns
> A2 into , [1,0] and I need a simple way to do that..
> 
> ( The way this works is A->0 and 2->1 -- Yes they interchange --  So
> B14 == [13,1] )

why didn't you say this in the first place?

def coord2tuple(coord):
    row, col = '', ''
    alpha = 'abcdefghijklmnopqrstuvwxyz'.upper()
    pairs = [''.join((x,y)) for x in alpha for y in [''] + [z for z in alpha]]
    pairs = sorted(pairs, key=len)
    coord = coord.upper()
    for c in coord:
        if c in alpha:
            row += c
        else:
            col += c
    return (int(col)-1, pairs.index(row))

>>> coord2tuple('B14')
(13, 1)
>>> coord2tuple('ZZ14')
(13, 701)
>>> coord2tuple('ZZ175')
(174, 701)
>>> coord2tuple('A2')
(1, 0)

Are there cols greater than ZZ? I seem to remember that there are not,
but I could be wrong.

Hope this helps.

Peace
Bill Mill
bill.mill at gmail.com



More information about the Python-list mailing list