user3412839
user3412839

Reputation: 576

Reading a file and adding to a list once it reaches an empty line

I have a method which reads a file as described below and adds each entry of first name, last name up until the specified size into a list and returns the list.

The file looks something like this:

Smith
John
newline
Jeff
Johnny
newline

where we have first name, last name, a blank line, then the next entry of names while the last line in the text file is a blank line as well. So if we set the size to 1, then only the first entry, so, [Smith, John] should be added to a list and then returned. If the size was 2 then it should return two entries, so [['Smith', 'John'], ['jeff', 'johnny']]

My attempt:

def addToList(file, size):
    newList = []
    temp = []
    with open(file, "r") as f:
        # read each line in the file
        for line in f:
            # if the len of the newlist is less then the size then check
            # to see if its either a new line or not.
            if len(newList) < size:
                # if its not a new line than add the line to a temp list
                if line != "\n":
                    temp.append(line.strip())
                # if it is then that means we're going to move on
                # to a new entry so add this entry to the newList
                # and  reset the temp list.
                else:
                    newList.append(temp)
                    temp = []
    return newList

So I run addToList("random.txt", 1) I get [['Smith', 'John']] which is the correct result, since size = 1 = one entry. However, the problem with what I have is that if I set the size to be 2, so if I do addToList("random.txt", 2) I only get the list with the first entry of names rather then the 2 entries. So what I get is this: [['Smith', 'John']] rather than [['Smith', 'John'], ['jeff', 'johnny']] It seems like it doesn't read the last empty line, but how would I fix that? If anyone can help me out that would be really appreciated.

Examples for clarification:

Lets say text file is this:

Smith
John
emptyline
John
Smith
emptyline
Smith
John
emptyline
John
Jeff
emptyline

results should be:

addToList("random.txt", 1) -> [['Smith', 'John']] because size = 1 = # of entry = 1
addToList("random.txt", 2) -> [['Smith', 'John'], ['John', 'Smith']]
addToList("random.txt", 3) -> [['Smith', 'John'], ['John', 'Smith'], ['Smith', 'John']]
addToList("random.txt", 4) -> [['Smith', 'John'], ['John', 'Smith'], ['Smith', 'John'], ['John', 'Jeff']]

Upvotes: 0

Views: 371

Answers (2)

Adam Smith
Adam Smith

Reputation: 54162

The simple way is to build an accumulator that grabs each non-blank line, appending it to a result when it encounters a blank line.

def read_file(path):
    with open(path) as inf:
        result = []
        group = []
        for line in inf:
            line = line.strip()
            if not line: # blank line
                result.append(group)
                group = []
            else:
                group.append(line)
    return result

The another option is:

def read_file(path, groupsize):
    with open(path) as inf:
        while True:
            try:
                yield [next(inf).strip() for _ in range(groupsize)]
            except StopIteration:
                # EOF
                return
            next(inf) # skip the blank line

Upvotes: 0

Avinash Raj
Avinash Raj

Reputation: 174696

You could do like the below,

with open('file') as f:
    fil = f.read()
    print([i.split('\n') for i in fil.split('\n\n') if i])

Output:

[['Smith', 'John'], ['Jeff', 'Johnny']]

Upvotes: 2

Related Questions