简体   繁体   English

IndexError:从文件中检索文本时,列表索引超出范围

[英]IndexError : list index out of range when retrieving text from a file

I'm trying to create a quiz in python, and I need to retrieve more than one question from an external text file. 我正在尝试在python中创建测验,并且我需要从外部文本文件中检索多个问题。 I manage to retrieve the first question successfully, but I get the "list index out of range" error when trying to retrieve the second one. 我设法成功检索了第一个问题,但是在尝试检索第二个问题时出现“列表索引超出范围”错误。

This is what a fragment of my current code looks like. 这就是我当前代码的一部分。

if choice1 == "CH":
        choice2 = input ("Would you like to do the easy, medium or hard questions ?").lower()
        if choice2 == "easy":
            load_profile = open("chemistryquiz.txt","r")
            question1 = load_profile.read().splitlines()[4]
            print (question1)
            question2 = load_profile.read().splitlines()[5]
            print (question2)

The program works perfectly fine if I comment out anything regarding question 2. Where have I gone wrong ? 如果我对问题2提出任何评论,则该程序可以正常运行。哪里出错了? PS, I checked the text file and I made sure the number of the line is 5, I am aware you start counting from 0 when programming in python. PS,我检查了文本文件,并确保行号是5,我知道您使用python编程时从0开始计数。

Also, these are the contents of chemistryquiz.txt 另外,这些是chemistryquiz.txt的内容

Chemistry Quiz :

Easy :

1) What is the chemical symbol of Carbon ? A: C B: Ca
2) What is the weight of an electron ? A: 0 B: 0.1

Let's step through this: 让我们逐步完成:

load_profile = open("chemistryquiz.txt","r")

The file is open; 该文件已打开; and you have a file handle load_profile . 并且您有一个文件句柄load_profile The file bookmark is at the start of the file. 文件书签位于文件的开头。

question1 = load_profile.read().splitlines()[4]

You have read the entire file, split it into lines, and assigned the 5th line to question1 . 您已经阅读了整个文件,将其分成几行,并将第5行分配给question1

print (question1)
question2 = load_profile.read().splitlines()[5]

Since the bookmark is still at the end of the file, read() returns only EOF. 由于书签仍位于文件末尾,因此read()仅返回EOF。 splitlines does nothing useful. 分割线没有任何用处。 There is no element 5. KABOOM! 没有元素5. KABOOM! .


Go back to the text on reading lines from a file. 返回到从文件读取行的文本。 For instance ... 例如 ...

with open("chemistryquiz.txt","r") as load_profile:
    for input_line in load_profile:
        # This loop will give you the file, one line at a time.

Thye problem is that you're calling load_profile.read() multiple times. 您的问题是您多次调用load_profile.read() Each time you call this, it starts reading from where any previous file reading functions left off. 每次您调用它时,它都会从任何先前的文件读取功能停止的地方开始读取。 But the first call read the entire file, so the second call has nothing left to read. 但是第一个调用读取了整个文件,因此第二个调用没有读取的内容。 It returns an empty string, and splitlines() returns an empty list. 它返回一个空字符串,而splitlines()返回一个空列表。

Just read the file once. 只需读取一次文件。

lines = load_profile.read().splitlines()
question1 = lines[4]
question2 = lines[5]

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM