简体   繁体   English

获取 Excel 列 label (A, B, ..., Z, AA, ..., AZ, BA, ..., ZZ, AAA, AAB, ...)

[英]Get the Excel column label (A, B, ..., Z, AA, ..., AZ, BA, ..., ZZ, AAA, AAB, ...)

Given the letter(s) of an Excel column header I need to output the column number.给定 Excel 列 header 的字母,我需要 output 列号。

It goes AZ , then AA-AZ then BA-BZ and so on.它是AZ ,然后是AA-AZ然后是BA-BZ等等。

I want to go through it like it's base 26, I just don't know how to implement that.我想通过它 go 就像它是 26 进制一样,我只是不知道如何实现它。

It works fine for simple ones like AA because 26^0 = 1 + 26^1 = 26 = 27 .它适用于像AA这样的简单算法,因为26^0 = 1 + 26^1 = 26 = 27

But with something like ZA , if I do 26 ^ 26 (z is the 26th letter) the output is obviously too large.但是对于像ZA这样的东西,如果我做26 ^ 26 (z 是第 26 个字母),output 显然太大了。 What am I missing?我错过了什么?

If we decode "A" as 0, "B" as 1, ... then "Z" is 25 and "AA" is 26.如果我们将“A”解码为 0,“B”解码为 1,……那么“Z”为 25,“AA”为 26。

So it is not a pure 26-base encoding, as then a prefixed "A" would have no influence on the value, and "AAAB" would have to be the same as "B", just like in the decimal system 0001 is equal to 1. But this is not the case here.所以它不是纯 26 基编码,因为前缀“A”对值没有影响,“AAAB”必须与“B”相同,就像十进制系统中 0001 相等到 1. 但这里不是这样。

The value of "AA" is 1*26 1 + 0, and "ZA" is 26*26 1 + 0. “AA”的值为 1*26 1 + 0,“ZA”的值为 26*26 1 + 0。

We can generalise and say that "A" should be valued 1, "B" 2, ...etc (with the exception of a single letter encoding).我们可以概括并说“A”的值应该是 1,“B”应该是 2,……等等(除了单个字母编码)。 So in "AAA", the right most "A" represents a coefficient of 0, while the other "A"s represent ones: 1*26 2 + 1*26 1 + 0所以在“AAA”中,最右边的“A”代表系数为0,而其他的“A”代表系数:1*26 2 + 1*26 1 + 0

This leads to the following code:这导致以下代码:

def decode(code):
    val = 0
    for ch in code: # base-26 decoding "plus 1"
        val = val * 26 + ord(ch) - ord("A") + 1 
    return val - 1

Of course, if we want the column numbers to start with 1 instead of 0, then just replace that final statement with:当然,如果我们希望列号以 1 而不是 0 开头,那么只需将最后的语句替换为:

return val

sum of powers权力总和

You can sum the multiples of the powers of 26:您可以将 26 的幂的倍数相加:

def xl2int(s):
    s = s.strip().upper()
    return sum((ord(c)-ord('A')+1)*26**i
               for i,c in enumerate(reversed(s)))

xl2int('A')
# 1

xl2int('Z')
# 26

xl2int('AA')
# 27

xl2int('ZZ')
# 702

xl2int('AAA')
# 703

int builtin int内置

You can use a string translation table and the int builtin with the base parameter.您可以使用字符串转换表和带有base参数的int内置函数。

As you have a broken base you need to add 26**n+26**(n-1)+...+26**0 for an input of length n, which you can obtain with int('11...1', base=26) where there are as many 1s as the length of the input string.由于您的基础已损坏,您需要为长度为 n 的输入添加 26**n+26**(n-1)+...+26**0,您可以使用int('11...1', base=26)其中 1 的数量与输入字符串的长度一样多。

from string import ascii_uppercase, digits
t = str.maketrans(dict(zip(ascii_uppercase, digits+ascii_uppercase)))

def xl2int(s):
    s = s.strip().upper().translate(t)
    return int(s, base=26)+int('1'*len(s), base=26)

xl2int('A')
# 1

xl2int('Z')
# 26

xl2int('AA')
# 27

xl2int('ZZ')
# 702

xl2int('AAA')
# 703

How the translation works翻译的工作原理

It shifts each character so that A -> 0, B -> 1... J -> 9, K -> A... Z -> P. Then it converts it to integer using int .它移动每个字符,以便 A -> 0, B -> 1...J -> 9, K -> A... Z -> P。然后它使用int将其转换为整数。 However the obtained number is incorrect as we are missing 26**x for each digit position in the number, so we add as many power of 26 as there are digits in the input.但是得到的数字是不正确的,因为我们在数字中的每个数字位置都缺少 26**x,所以我们添加了与输入中的数字一样多的 26 的幂。

Another way to do it, written in VBA:另一种方法,写在 VBA 中:

Function nColumn(sColumn As String) As Integer

' Return column number for a given column letter.

' 676 = 26^2
' 64 = Asc("A") - 1

nColumn = _
    (IIf(Len(sColumn) < 3, 0, Asc(Left(      sColumn    , 1)) - 64) * 676) + _
    (IIf(Len(sColumn) = 1, 0, Asc(Left(Right(sColumn, 2), 1)) - 64) * 26) + _
                             (Asc(     Right(sColumn    , 1)) - 64)

End Function

Or you can do it directly in the worksheet:或者您可以直接在工作表中进行:

=(if(len(<clm>) < 3, 0, code(left(      <clm>    , 1)) - 64) * 676) + 
 (if(len(<clm>) = 1, 0, code(left(right(<clm>, 2), 1)) - 64) * 26) + 
                       (code(     right(<clm>    , 1)) - 64)

I've also posted the inverse operation done similarly .我还发布了以类似方式完成的逆运算

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 a、b、aa、ab、ba、bb、aaa、aab 的生成器 - generator for a, b, aa, ab, ba, bb, aaa, aab 如何生成从aa…到zz的字符串范围 - How to generate a range of strings from aa… to zz 如何在这个问题中迭代。实际上我发现从 A 到 Z 我们会收到索引 1-26,从 AA 到 ZZ 是 26+26^2。但是如何进行? - HOW TO ITERATE IN THIS QUESTION.ACTUALLY I FOUND THAT FROM A to Z WE WOULD RECIEVE INDEX 1-26 AND FROM AA TO ZZ IT IS AS 26+26^2.BUT HOW TO PROCEED? 在Python中将&#39;aa&#39;转换为&#39;a&#39;并将&#39;ab&#39;转换为&#39;b&#39; - translate 'aa' to 'a' and 'ab' to 'b' in python 制作了一个递归程序,将列位置转换为Excel列(1 = A,27 = AA),在输出中得到@ - Made a recursive program to convert the column position into an Excel column (1 = A, 27 = AA), getting @'s in my output 结果要写在excel列B中 - results to be written in excel column B 如何在python中编写字母bigram(aa,ab,bc,cd…zz)频率分析计数器? - How to write a alphabet bigram (aa, ab, bc, cd … zz) frequency analysis counter in python? 为什么 &#39;aa&#39; &lt; &#39;z&#39; 在 Python 中的计算结果为 True? - Why does 'aa' < 'z' evaluate to True in Python? 引用 Z 以外的字母表……即 AA、AB - Referencing alphabet beyond Z… ie AA, AB 如何获取列值的频率计数,按另一列中的分类值排序 - How to get frequency count for a column value, sorted by aa categorical value in another column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM