Python初学者遇到的基础问题

这个代码没有问题,主要就是将txt文件中内容,每行以列表等不同形式输出,现在要将读取代码数据操作并将其处理到parts变量中,但将数据作为列表,在get_data函数 列表执行此修改,return 出[['CP1401', 'Ada Lovelace', 192],['CP1404', 'Alan Turing', 98]]这种

FILENAME = "subject_data.txt"


def main():
    data = get_data()
    print(data)


def get_data():
    """Read data from file formatted like: subject,lecturer,number of students."""
    input_file = open(FILENAME)

    for line in input_file:
        print(line)  # See what a line looks like
        print(repr(line))  # See what a line really looks like
        line = line.strip()  # Remove the \n
        parts = line.split(',')  # Separate the data into its parts
        print(parts)  # See what the parts look like (notice the integer is a string)
        parts[2] = int(parts[2])  # Make the number an integer (ignore PyCharm's warning)
        print(parts)  # See if that worked
        print("----------")
    input_file.close()


main()

#txt文件内容
CP1401,Ada Lovelace,192
CP1404,Alan Turing,98
CP4321,Bill Gates,676
CP1234,Steve Jobs,123

代码和运行截图如下:有帮助的话记得采纳一下!

FILENAME = "subject_data.txt"

def main():
    data = get_data()
    print(data)

def get_data():
    """Read data from file formatted like: subject,lecturer,number of students."""
    input_file = open(FILENAME)
    list = []
    for line in input_file:
        print(line)  # See what a line looks like
        print(repr(line))  # See what a line really looks like
        line = line.strip()  # Remove the \n
        parts = line.split(',')  # Separate the data into its parts
        print(parts)  # See what the parts look like (notice the integer is a string)
        parts[2] = int(parts[2])  # Make the number an integer (ignore PyCharm's warning)
        print(parts)  # See if that worked
        print("----------")
        list.append(parts)
    input_file.close()
    return list

main()

img