python中将数字转换为字母_用python把数字转换成字母

方法一(普通方法):

# -*- coding: utf-8 -*-

#把数字转换成相应的字符,1-->'A' 27-->'AA'

def changeNumToChar(toSmallChar=None, toBigChar=None):

#n = toSmallChar and toSmallChar or toBigChar

#c = toSmallChar and ord('A')-1 or ord('a')-1

init_number = 0

increment = 0

res_char = ''

if not toSmallChar and not toBigChar:

return ''

else:

if toSmallChar:

init_number = toSmallChar

increment = ord('A') - 1

else:

init_number = toBigChar

increment = ord('a') - 1

shang,yu = divmod(init_number, 26)

char = chr(yu + increment)

res_char = char*(shang + 1)

#for i in range(shang + 1):

# char = chr(yu + increment)

# res_char += cha

你可能感兴趣的:(python中将数字转换为字母)