Get Excel-Style Column Names from Column Number

前端 未结 8 1590
慢半拍i
慢半拍i 2020-12-01 12:38

This is the code for providing the COLUMN name when the row and col ID is provided but when I give values like row = 1 and col = 104, it should return CZ<

8条回答
  •  青春惊慌失措
    2020-12-01 13:02

    I think i figured it out. divmod(104,26) gives mod=0 which makes chr(0+64) = 64 ie '@'.

    if i add this line before column_label "mod=26 if mod==0 else mod" i think it should work fine

    column_label=''
    div=104
    while div:
        (div, mod) = divmod(div, 26)
        mod=26 if mod==0 else mod
        column_label = chr(mod + 64) + column_label
    
    print column_label
    

提交回复
热议问题