使用python

时间:2017-02-25 17:32:18

标签: python-2.7 parsing string-parsing

我是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

我该怎么做?任何帮助表示赞赏。谢谢。

1 个答案:

答案 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"),查看结果并确保了解发生了什么。