我试图在python中获取文本文件段落的开始和结束偏移量。我尝试了下面的代码,它给出了开始和结束偏移量,但如果段落以空格或制表符开头,则不会将其视为段落。在
paraStartOffset = []
paraEndOffset = []
for match in re.finditer(r'(?s)((?:[^\n]?)+)', textFile):
paraStartOffset.append(match.start())
paraEndOffset.append(match.end())
print "start Offset --> ",paraStartOffset
print "end Offset --> ",paraEndOffset
有人能告诉我我在哪里错过了什么。谢谢。在
我想这篇question / answer基本上讨论了你在找什么。 如果我在段落开头也使用前导空格测试代码(取自答案),那么它几乎可以工作。在
当我在我的测试文本(取自Bram Stoker's Dracula)上运行它时,它返回以下结果:第一段是上的标准。第二个从空格开始。第三个以TAB开头。在
结果:(显示每个段落的起始偏移量和结束偏移量)
^{pr2}$测试文本:(我无法获得与原始格式完全相同的格式,但无论如何…)
相关问题 更多 >
编程相关推荐