当前位置:  首页>> 技术小册>> Python合辑3-字符串用法深度总结

str.splitlines(keepends=False)

有时想处理一个在边界处具有不同换行符(’\n’、\n\n’、’\r’、’\r\n’)的语料库。要拆分成句子,而不是单个单词。可以使用 splitline 方法来执行此操作。当 keepends=True 时,文本中包含换行符;否则它们被排除在外

  1. import nltk # You may have to `pip install nltk` to use this library.
  2. macbeth = nltk.corpus.gutenberg.raw('shakespeare-macbeth.txt')
  3. print(macbeth.splitlines(keepends=True)[:5])

Output:

  1. ['[The Tragedie of Macbeth by William Shakespeare 1603]\n', '\n', '\n', 'Actus Primus. Scoena Prima.\n', '\n']

该分类下的相关小册推荐: