read file into array separated by paragraph Python

后端 未结 7 962
半阙折子戏
半阙折子戏 2021-02-08 22:11

I have a text file, I want to read this text file into 3 different arrays, array1 array2 and array3. the first paragraph gets put in array1, the second paragraph gets put in arr

7条回答
  •  借酒劲吻你
    2021-02-08 22:30

    More elegant way to bypass slices:

    def grouper(n, iterable, fillvalue=None):
        args = [iter(iterable)] * n
        return itertools.izip_longest(fillvalue=fillvalue, *args)
    
    for p in grouper(5,[sent.strip() for sent in text.split('\n') if sent !='']):
        print p
    

    Just make sure you deal with None in final text

提交回复
热议问题