Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

read file into array separated by paragraph Python

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 array2 and so on. the 4th paragraph will then be put in array1 element2 and so forth, paragraphs are separated by a blank line. any ideas?

like image 574
quantumdisaster Avatar asked Nov 27 '11 01:11

quantumdisaster


5 Answers

This is the basic code I would try:

f = open('data.txt', 'r')

data = f.read()
array1 = []
array2 = []
array3 = []
splat = data.split("\n\n")
for number, paragraph in enumerate(splat, 1):
    if number % 3 == 1:
        array1 += [paragraph]
    elif number % 3 == 2:
        array2 += [paragraph]
    elif number % 3 == 0:
        array3 += [paragraph]

This should be enough to get you started. If the paragraphs in the file are split by two new lines then "\n\n" should do the trick for splitting them.

like image 112
Codahk Avatar answered Oct 01 '22 19:10

Codahk


import itertools as it


def paragraphs(fileobj, separator='\n'):
    """Iterate a fileobject by paragraph"""
    ## Makes no assumptions about the encoding used in the file
    lines = []
    for line in fileobj:
        if line == separator and lines:
            yield ''.join(lines)
            lines = []
        else:
            lines.append(line)
    yield ''.join(lines)

paragraph_lists = [[], [], []]
with open('/Users/robdev/Desktop/test.txt') as f:
    paras = paragraphs(f)
    for para, group in it.izip(paras, it.cycle(paragraph_lists)):
        group.append(para)

print paragraph_lists
like image 38
Rob Cowie Avatar answered Oct 01 '22 18:10

Rob Cowie


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.

like image 45
JKC Avatar answered Oct 01 '22 17:10

JKC


Because I feel like showing off:

with open('data.txt') as f:
    f = list(f)
    a, b, c = (list(__import__('itertools').islice(f, i, None, 3)) for i in range(3))
like image 36
Karl Knechtel Avatar answered Oct 01 '22 17:10

Karl Knechtel


This code will search for lines between two points:

rr = [] #Array for saving lines    
for f in file_list:
    with open(f, 'rt') as fl:
        lines = fl.read()
        lines = lines[lines.find('String1'):lines.find('String2')] 
        rr.append(lines)
like image 37
CPSO Avatar answered Oct 01 '22 19:10

CPSO