Mattia Spadoni
Mattia Spadoni

Reputation: 21

python: index out of range in txt file

I'm writing a script to automatically annotate a txt file.

I open the txt file and segment it into a list of lines. Then I iterate over every line. I want the PC to check if the previous element in the list (the line before in the text) is an empty element (the paragraph division in the text) and if it so to put an annotation.

final_list = []
something = open(x, 'r', encoding='utf8', errors='ignore')
file = something.read()
y = file.split("\n")
for position, i in enumerate(y):
    if position == 0:
        final_list.append(i)
    elif position > 0:
        z = i[position-1]
        if z == '':
            final_list.append("<p>"+i)
return final_list

I expect to a have a final list with all the element of the previous line with some of them marked with the

element, but when I iterate over the list Python gives me a

IndexError: string index out of range

I cannot understand where is the problem.

Upvotes: 0

Views: 153

Answers (2)

Lo&#239;c
Lo&#239;c

Reputation: 11943

How about something like this :

last_line = ''
output_lines = []
with open('file.txt', 'r') as f:
    for line in f:
        line = line.strip()
        if last_line == '':  # if last line was empty, start a new paragraph
            output_lines.append('<p>')
            output_lines.append(line)
        elif line == '':  # if current line is empty close the paragraph
            output_lines.append('</p>')
        else:
            output_lines.append(line)        
        last_line = line

Upvotes: 0

shaik moeed
shaik moeed

Reputation: 5785

As you are not using values of list, instead of enumerate take length of list and iterate.

You can try this,

for position in range(len(y)):
        if position == 0:
            final_list.append(i)
        elif position > 0:
            z = y[position-1]
            if z == '':
                final_list.append("<p>"+i)

Upvotes: 0

Related Questions