简体   繁体   English

python:索引超出txt文件的范围

[英]python: index out of range in txt file

I'm writing a script to automatically annotate a txt file. 我正在编写一个脚本来自动注释txt文件。

I open the txt file and segment it into a list of lines. 我打开txt文件并将其分割成一个行列表。 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. 我希望PC检查列表中的前一个元素(文本中的前一行)是否为空元素(文本中的段落除法),以及是否放置注释。

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 元素,但是当我遍历列表时,Python给了我一个

IndexError: string index out of range IndexError:字符串索引超出范围

I cannot understand where is the problem. 我无法理解问题出在哪里。

As you are not using values of list, instead of enumerate take length of list and iterate. 因为你没有使用list的值,而不是enumerate取列表的长度和迭代。

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)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 python 中对 txt 文件进行排序时,字符串索引超出范围 - string index out of range when sorting txt file in python Python读取txt文件作为输入:IndexError:列表索引超出范围? - Python read txt file as input: IndexError: list index out of range? 尝试读取 Python 中的 txt 文件时,列表索引超出范围 - List index out of range when trying to read in a txt file in Python IndexError:列表索引超出范围-无法获取我的txt文件以将输出保存在Python中 - IndexError: list index out of range - Can't get my txt file to save the output in Python IndexError:读取大的.txt文件时,Python的字符串索引超出范围 - IndexError: string index out of range with Python when reading big .txt file IndexError:元组索引超出范围,写入 txt 文件 - IndexError: tuple index out of range with write to txt file 将数据从 txt 文件传输到数组(列表索引超出范围) - transfering data from txt file to an array (list index out of range) 打开和打印 txt 文件时出现“IndexError: list index out of range” - "IndexError: list index out of range" when opening and printing a txt file 列表索引超出&#39;file&#39;对象的范围 - Python - List index out of range in 'file' object — Python Python-列表索引超出范围csv文件 - Python - List index out of range csv file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM