在不使用枚举的情况下找到匹配模式的行号

时间:2018-07-04 09:33:27

标签: python text-processing enumerate

我想在文件中查找模式的行号而不使用枚举。 是否有直接提供模式行号的Python库?

1 个答案:

答案 0 :(得分:1)

您可以这样做

>>> content = open('your_file').read()
>>> content[:content.find(your_pattern)].count('\n') + 1
line number of your_pattern in your_file

假设包含your_pattern。 如果很清楚您为什么需要知道这一点以及实际的问题是什么,我可以为您提供更多帮助。

相关问题