read file into array separated by paragraph Python

后端 未结 7 963
半阙折子戏
半阙折子戏 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:37

    Using slices would also work.

    par_separator = "\n\n"
    paragraphs = "1\n\n2\n\n3\n\n4\n\n5\n\n6".split(par_separator)
    a,b,c = paragraphs[0:len(paragraphs):3], paragraphs[1:len(paragraphs):3],\
            paragraphs[2:len(paragraphs):3] 
    

    Within slice: [start index, end index,step]

提交回复
热议问题