将字符串中字符的索引转换为字符串中字符对应的行和列

Converting index of character in a strign to the corresponding line and column of the character in the string

问题

是否有任何模块有一个方便的函数,它接受一个字符串和其中的字符索引以及returns字符串中字符的行号和列号?

例子

例如,我有这个字符串,行号写在每行前面的方括号中,并用 space:

分隔

[1] Lorem ipsum dolor sit amet, consectetur

[2] sit amet hendrerit lorem auctor. Nulla in

[3] egestas diam, sed semper justo. Ut risus nulla,

[4] vulputate eget tempor in, tempor sit amet orci.

[5] Etiam sollicitudin porta odio, eu dignissim est

我想获取第 45 个字符的行号和列。它将是第 2 行第 6 个字符(如果我计算正确的话)

为什么我需要这个

我正在解析一个字符串,我想在无法解析某些内容时引发异常。以更 CS 的方式:我想解析一个包含某种语言代码的字符串,如果代码中的某些内容违反了该语言的规则,我想对此提出异常。我想在异常消息中包含字符的行号和列号,但我只有字符的索引。

这似乎是标准 Python 库的简单应用。可能库没有足够方便的接口,但是写起来很容易:

def line_col(str, idx):
    """Returns a 2-tuple which translates string index 'idx' into
       line and column counts. 'idx' is a normal Python 0-based index;
       line and column are 1-based. So line_col(str, 0) will return (1, 1).
       The newline character is considered to be the last character of a
       line.
       The function does not check whether 'idx' is in range; if idx greater
       than or equal to the length of 'str', the result will be as though
       'str' had (idx + 1 - len(str)) extra characters (none of which are
       newlines) appended to the end
    """
    return str.count('\n', 0, idx) + 1, idx - str.rfind('\n', 0, idx)