简体   繁体   English

尝试读取 Python 中的 txt 文件时,列表索引超出范围

[英]List index out of range when trying to read in a txt file in Python

I want to read a txt file in Python.我想在 Python 中读取一个 txt 文件。 The text file consists of 3 columns, all are separated by a ','.文本文件由 3 列组成,所有列都用“,”分隔。 One column represents the ID, one for a random float number, and one for the assigned attribute (solo, not solo, acc).一列表示 ID,一列表示随机浮点数,一列表示分配的属性(solo,not solo,acc)。

My goal is to convert values for 'solo' and 'acc' to 'not solo', and afterwards calculate the variable y.我的目标是将“solo”和“acc”的值转换为“not solo”,然后计算变量 y。

The user is asked to enter an ID (number) in. Then I want to convert the lines of the txt file into a list and search for the remaining values, eg User enters in: 45 --> I want to search for the assigned value, then convert it to 'not solo' and calculate y.要求用户输入 ID(数字)。然后我想将 txt 文件的行转换为列表并搜索剩余的值,例如用户输入:45 --> 我想搜索分配的值,然后将其转换为 'not solo' 并计算 y。 In the end, I want to print the assigned value in the unit 'not solo' as well as the y-calculation.最后,我想以“非独奏”为单位打印分配的值以及 y 计算。

However, when I am trying to find the values, I get an error saying that my list index would be out of range.但是,当我尝试查找值时,我收到一条错误消息,指出我的列表索引超出范围。 Does anyone have a clue how to fix this?有谁知道如何解决这个问题?

I am browsing for hours but I can't find a solution that works.我浏览了几个小时,但找不到有效的解决方案。

Thank you so much!太感谢了!

import math

fobj = open('test.txt', 'r')
file_content = fobj.readline()
fobj.close()

for file in file_content:

file = file.strip()
file_list = file.split(',')

ID_q = int(input("Please enter number [else enter 'finished']:"))
ID = ID_q + 1

if ID_q != "finished":
    output = "Your ID {} is assigned to following values: ".format(ID_q)
    print(output)

    #Now the fun part starts!
    chosen_line = [ID]

    for position, line in enumerate(file_content):
        if position in chosen_line:
            file_line = chosen_line.split(',')

            ID_inline = file_list[0]
            print(ID_inline)

            if file_list[2] == 'solo':
                x = float((float(file_list[1])) * (math.pi/180))
                x_result = float("not solo: {0}".format(x))
                x_result = round(x_result, 2)

                y = float(math.sin((6*(float(file_list[1])))-(3*math.cos((float(file_list[1]))))))
                function = float("f(x) = {0}".format(y))
                function = round(function, 2)

                print(x_result)
                print(function)

            elif file_list[2] == 'acc':
                x = float((float(file_list[1])) * (math.pi/200))
                x_result = float("not solo: {0}".format(x))
                x_result = round(x_result, 2)

                y = float(math.sin((6*(float(file_list[1])))-(3*math.cos(float(file_list[1])))))
                function = float("f(x) = {0}".format(y))
                function = round(function, 2)

                print(x_result)
                print(function)

            else:
                x = ID
                y = float(math.sin((6*(float(file_list[1])))-(3*math.cos(float(file_list[1])))))
                function = float("f(x) = ".format(y))
                function = round(function, 2)

                print(x)
                print(function)

else:
    print("Bye.")

When you do this:当你这样做时:

fobj = open('test.txt', 'r')
file_content = fobj.readline()
fobj.close()

You are reading only one line, not the whole file.您只读取一行,而不是整个文件。

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

相关问题 Python读取txt文件作为输入:IndexError:列表索引超出范围? - Python read txt file as input: IndexError: list index out of range? 在 python 中对 txt 文件进行排序时,字符串索引超出范围 - string index out of range when sorting txt file in python IndexError:列表索引超出范围。 (尝试查找一个txt文件的元素并将其替换为另一个txt文件 - IndexError: list index out of range. (trying to find and substitute elements of one txt file with another txt file 试图从 CSV 文件中读取数据,列表索引超出范围? - Trying to read in data from a CSV file, list index out of range? IndexError:尝试读取文本文件时列表索引超出范围 - IndexError: list index out of range while trying to read a text file python:索引超出txt文件的范围 - python: index out of range in txt file 文件读取 Python - IndexError:列表索引超出范围 - File read Python - IndexError: list index out of range 尝试保存新文件时发生Python错误:IndexError:列表索引超出范围 - Python error when trying to save a new file: IndexError: list index out of range 如何交换从列表.txt文件读取的数字? 错误“列表索引超出范围” - How to swap numbers read from a list .txt file? Error “list index out of range” 获取:尝试读取文本文件时出现索引超出列表错误 - Getting: index out of list error when trying to read text file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM