【问题标题】:Find the line number containing a given span查找包含给定跨度的行号
【发布时间】:2019-04-28 16:38:48
【问题描述】:

给定一个文本文件,如何找到与给定子字符串的开始和结束索引对应的行号?即使可以找到任何给定跨度的值,我正在使用的数据集也包含重复项。

【问题讨论】:

  • 您能否更具体一些,并提供示例输入以及您希望看到的内容。另外,请说明您到目前为止所做的工作。
  • 我有一个文本文件,其中包含字符 1673 和 1687 之间的短语“两性霉素 B”。我需要一个函数,给定文件的文本以及开始和结束索引,返回行号该短语的确切实例。

标签: python file indices


【解决方案1】:

line_number = my_string[:start_index].count("\n")

我猜……

my_string = """
hello
test
line 
numbers
"""

print(my_string[:14].count("\n")) # the 14th character is the 3rd line

【讨论】:

    猜你喜欢
    • 2013-05-13
    • 2021-04-23
    • 2016-02-09
    • 2011-04-17
    • 1970-01-01
    • 2011-09-03
    • 2015-04-06
    • 2016-09-11
    • 1970-01-01
    相关资源
    最近更新 更多