python - 查找包含给定跨度的行号
问题描述
给定一个文本文件,如何找到与给定子字符串的开始和结束索引对应的行号?即使可以找到任何给定跨度的值,我正在使用的数据集也包含重复项。
解决方案
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
推荐阅读
- python - 功能修改原图
- python - 无法安装lxml
- python - python的初学者问题:如何在python的文件中制作一个分隔行列表
- php - 在 Woocommerce 中按星级 ASC 添加产品排序选项
- .net-core - Windows 和 Linux 上的 Net Core Worker 服务
- c++ - Qt C++ 是静态thread_local QNetworkAccessManager 线程应用程序的好选择吗?
- python - 从 Python 中的两个数据数组中检测模式
- java - 数据库访问和模型在内存中的存储
- python-3.x - 如何根据熊猫中的多个条件返回布尔系列?
- java - Kafka 配置未在 Spring Boot 中从 Spring 配置服务器加载