Python 3.x 如何在python中剥离行以允许使用从txt文件中提取的列表中的数字进行计算?

Python 3.x 如何在python中剥离行以允许使用从txt文件中提取的列表中的数字进行计算?,python-3.x,Python 3.x,我正在打开一个.txt文件,该文件在单独的行中列出以下数字: 86 92 77 83 96 写入文件后,数字将添加到主函数中的列表中,然后第二个函数需要查找列表中数字的平均值。代码需要能够处理原始.txt文件以外的数字,因为这是评分的方式。当我尝试剥离列表以收集总和和len时,它会向我提供以下错误: TypeError:不支持+:“int”和“str”的操作数类型 如果在剥离后使用打印(分数),则值如下所示: ['86','92','77','83','96'] 我认为问题在于列表中的“”,但似

我正在打开一个.txt文件,该文件在单独的行中列出以下数字: 86 92 77 83 96 写入文件后,数字将添加到主函数中的列表中,然后第二个函数需要查找列表中数字的平均值。代码需要能够处理原始.txt文件以外的数字,因为这是评分的方式。当我尝试剥离列表以收集总和和len时,它会向我提供以下错误:

TypeError:不支持+:“int”和“str”的操作数类型

如果在剥离后使用打印(分数),则值如下所示: ['86','92','77','83','96']

我认为问题在于列表中的“”,但似乎无法将其删除

这是我当前的代码:

def main():
scores = []
f = open("scores.txt", 'r')
line_list = list(f.readlines())
i = 0
while i < len(line_list):
    scores.append(line_list[i])
    i += 1
f.close()
showscores(scores)
def showscores(scores):
index = 0
while index < len(scores):
    scores[index] = scores[index].strip('\n')
    index += 1
sum(scores) 
def main():
分数=[]
f=打开(“scores.txt”,“r”)
line_list=list(f.readlines())
i=0
而i

main()。然后计算平均值。太棒了,谢谢!请注意:您不能“删除”引用。打印的是列表中数据的表示形式。您不会更改表示形式;您可以通过将字符串转换为整数来更改基础数据。
file = open("scores.txt", "r") # Open the file
lines = file.readlines() # Break the file up into lines
scores = []
for line in lines: # Loop through the lines
    data = line.split() # Split the data at each space
    for item in data: # Loop through that data
        scores.append(int(item)) # Convert to int before adding to scores
file.close() # Close file