参考文章:[PYTHON]26进制(A-Z)与10进制互相转换
Python实现excel的列名称转数字、26进制(A-Z)与10进制互相转换
sequence = list( map( lambda x: chr( x ), range( ord( 'A' ), ord( 'Z' ) + 1 ) ) )
##-----字母转数字(python实现 1-26=A-Z, then AA-AZ)
def ten2TwentySix(num):
L = []
num=num-1; #实现从1对应A
if num > 25:
while True:
d = int( num / 26 )
remainder = num % 26
if d <= 25:
L.insert( 0, sequence[remainder] )
L.insert( 0, sequence[d - 1] )
break
else:
L.insert( 0, sequence[remainder] )
num = d - 1
else:
L.append( sequence[num] )
return "".join( L )
def twentySix2Ten(s):
l = len( s )
sum = 1 #实现从A对应1
if l > 1:
for i in range( l - 1 ):
index = sequence.index( s[i] )
print( index )
num = pow( 26, l - 1 ) * (index + 1)
print( num )
l = l - 1
sum = sum + num
sum = sum + sequence.index( s[-1] )
else:
sum = sum + sequence.index( s[-1] )
return sum
if __name__ == '__main__':
print( ten2TwentySix( 100 ) )
print( twentySix2Ten( 'Z' ) )
网友评论