您的位置:首页 > 编程语言 > Python开发

Python Excel 列名转换

2013-05-08 13:05 225 查看
def colname_to_num(colname):

if type(colname) is not str:

return colname

col = 0

power = 1

print len(colname)

for i in xrange(len(colname) - 1, -1, -1):

ch = colname[i]

print ch

col += (ord(ch) - ord('A') + 1 ) * power

power *= 26

print col-1

return col - 1

def colnum_to_name(colnum):

if type(colnum) != int:

return colnum

if colnum > 25:

ch1 = chr(colnum % 26 + 65)

ch2 = chr(colnum / 26 + 64)

print ch2+ch1

return ch2 + ch1

else:

print chr(colnum % 26 + 65)

return chr(colnum % 26 + 65)

if __name__ == "__main__":

colname_to_num("CU")

colnum_to_name(26)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: