我是python编程的新手。我想拆分一个有两个分隔符的文本文件。
我只是硬编码文本文件的一行以使逻辑正确。我想在分割文本后打印分隔符。不太清楚如何做到这一点。
text='+1 This book is awesome. It has been so helpful to be able to go back to track trends. -1 I bought this for my sister and she didn't like it. The note sections to t'
for line in text.split('+1'):
print line
这给了我没有+1的输出,但我希望输出像这样:
+1 This book is awesome. It has been so helpful to be able to go back to track trends.
-1 I bought this for my sister and she didn't like it. The note sections to t
我该怎么做?任何帮助表示赞赏。谢谢。
答案 0 :(得分:0)
查看find
字符串方法:https://docs.python.org/2.7/library/string.html#string.find
在之后,您了解它正在做什么,请使用text.find("+1")
和text.find("-1")
。
另外:split
无法按照您的想法运作。阅读文档(https://docs.python.org/2.7/library/string.html#string.split),运行"ab ac bc de".split("ab")
,查看结果并确保了解发生了什么。