例如,这是我的文档:
docs = '''
The goal of /r/Games is to provide a place for informative and interesting
gaming content and discussions. Submissions should be for the purpose of
informing or initiating a discussion, not just with the goal of entertaining
viewers.
IRC channel: #Games on irc.snoonet.org
For an in-depth explanation of our rules don't forget to check our rules
wiki page and FAQ!
I've been spending quite a while reading reviews of various players on the
game. And, it seems (for the most part) it's being received fairly
positively with gamers. I'm interested to hear how Reddit is enjoy the game
thus far. Feel free to be as brief, or as thorough as you want.
Thanks in advance for all replies. '''
我想将文档分隔成这样的列表:
sentence = [[The goal of /r/Games is to provide a place for informative and
interesting gaming content and discussions. Submissions should be for the
purpose of informing or initiating a discussion, not just with the goal of
entertaining viewers. IRC channel: #Games on irc.snoonet.org For an in-depth
explanation of our rules don't forget to check our rules wiki page and
FAQ!]],
[I've been spending quite a while reading reviews of various players on the
game. And, it seems (for the most part) it's being received fairly
positively with gamers. I'm interested to hear how Reddit is enjoy the game
thus far. Feel free to be as brief, or as thorough as you want.], [Thanks in
advance for all replies.]]
我用docs.split(' ')
只能得到一个字典,就像用单词分开一样
我如何获得sentence
?
答案 0 :(得分:1)
看起来您可以将其分成两行\n\n
docs = '''
The goal of /r/Games is to provide a place for informative and interesting
gaming content and discussions. Submissions should be for the purpose of
informing or initiating a discussion, not just with the goal of entertaining
viewers.
IRC channel: #Games on irc.snoonet.org
For an in-depth explanation of our rules don't forget to check our rules
wiki page and FAQ!
I've been spending quite a while reading reviews of various players on the
game. And, it seems (for the most part) it's being received fairly
positively with gamers. I'm interested to hear how Reddit is enjoy the game
thus far. Feel free to be as brief, or as thorough as you want.
Thanks in advance for all replies. '''
sentence = [i.strip() for i in docs.split('\n\n')]
for i in sentence:
print('i: ' + i + '\n')
输出:
i: The goal of /r/Games is to provide a place for informative and interesting
gaming content and discussions. Submissions should be for the purpose of
informing or initiating a discussion, not just with the goal of entertaining
viewers.
IRC channel: #Games on irc.snoonet.org
For an in-depth explanation of our rules don't forget to check our rules
wiki page and FAQ!
i: I've been spending quite a while reading reviews of various players on the
game. And, it seems (for the most part) it's being received fairly
positively with gamers. I'm interested to hear how Reddit is enjoy the game
thus far. Feel free to be as brief, or as thorough as you want.
i: Thanks in advance for all replies.
答案 1 :(得分:0)
似乎您想分割段落而不是句子。在这种情况下,请使用.split('\n\n')
,或更普遍地,请使用re.split(r'\n{2,}', docs)
,这将拆分为2个或更多'\n'