read file into array separated by paragraph Python

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

    I know this question was asked long before but just putting my inputs so that it will be useful to somebody else at some point of time. I got to know much easier way to split the input file into paragraphs based on the Paragraph Separator(it can be a \n or a blank space or anything else) and the code snippet for your question is given below :

    with open("input.txt", "r") as input:
        input_ = input.read().split("\n\n")   #\n\n denotes there is a blank line in between paragraphs.
    

    And after executing this command, if you try to print input_[0] it will show the first paragraph, input_[1] will show the second paragraph and so on. So it is putting all the paragraphs present in the input file into an List with each List element contains a paragraph from the input file.

提交回复
热议问题