[英]How to use split() to separate and display a text file to calculate and average
I'm having trouble with the split()
function. 我在
split()
函数时遇到麻烦。 I have a module named printReport()
that contains a name and 3 test scores in each line. 我有一个名为
printReport()
的模块,该模块在每行中包含一个名称和3个测试分数。 I need to split the data so the names and scores can be displayed and have the average calculated. 我需要拆分数据,以便可以显示名称和分数并计算出平均值。 I'm positive I'm trying to do this completely wrong.
我很肯定我正在尝试完全错误地做到这一点。 I'm getting an
IndexError: list index out of range.
我收到
IndexError: list index out of range.
This is only the start of my problem. 这仅仅是我的问题的开始。 I still have no idea how to do the calculations and be displayed like below.
我仍然不知道如何进行计算,并显示如下。
Student Name Score 1 Score 2 Score 3 Total
学生姓名分数1分数2分数3总计
Dave 82 91 77 250
戴夫82 91 77 250
Tom 79 22 84 185
汤姆79 22 84 185
Dick 67 22 91 180
迪克67 22 91180
Mon Feb 8 15:12:08 2016
2016年2月8日星期一15:12:08
Can someone please explain what I'm doing wrong and how I would fix it. 有人可以解释我在做什么错以及如何解决。
### Subprogram detStudentData(fn, scores)
def getStudentData(fn, scores):
# Writes instructions for the user to follow
print("Enter the student names and scores following the prompts below.")
print("To finish entering student names, enter: ZZZ.\n")
# Set the flag for the loop
done = False
# Loop to get input from the usere
while done != True:
# Creates a newFile and enables append
newFile = open(fn, "a")
# Asks for a student name and assigns it to student_name
student_name = input("Please enter the student name (ZZZ to finish): ")
# Compairs student_name to see if it is equal to "ZZZ"
if student_name == "ZZZ":
# Sets the flag to True to exit the loop
done = True
# Asks for test scores if student_name is not equal "ZZZ"
else:
# Asks for test score 1 and assigns it to test_1
test_1 = input("Enter score 1: ")
# Asks for test score 2 and assigns it to test_2
test_2 = input("Enter score 2: ")
# Asks for test score 3 and assigns it to test_3
test_3 = input("Enter score 3: ")
print("\n")
newFile.write(student_name) # Writes student_name to newFile
newFile.write(", ") # Writes "," to newFile
newFile.write(test_1) # Writes test_1 to newFile
newFile.write(", ") # Writes "," to newFile
newFile.write(test_2) # Writes test_2e to newFile
newFile.write(", ") # Writes "," to newFile
newFile.write(test_3) # Writes test_3 to newFile
newFile.write("\n") # Wites a return to newFile
# Closes newFile
newFile.close()
# ==============================================================================
### Subprogram getTextFileContents(fn)
def getTextFileContents(fn):
# Opens the file and enables read
with open(fn, "r") as ins:
# Splits the text file before the ","
list = fn.split(",")
# Creates a loop to load the characters into a list
for line in ins:
# Appends the text to list
list.append(line)
# Returns the value in list
return list
# ==============================================================================
### Subprogram printReport(line)
def printReport(line):
# Prints the heading to show the test scores
print("__________________________________________________________")
print("Student Name Score 1 Score 2 Score 3 Total")
print("----------------------------------------------------------")
name = [] # Declare name a list
test1 = [] # Declare test1 a list
test2 = [] # Declare test2 a list
test3 = [] # Declare test a list
with open("grades.txt", "r") as f:
for line in f:
name.append(line.split(",", 1)[0])
line = name[0]
capacity = len(name)
index = 0
while index != capacity:
line = name[index]
for nameOut in line.split():
print(nameOut)
index = index + 1
# ================================================
with open("grades.txt", "r") as f:
for line in f:
test1.append(line.split(",", -1)[1])
line = test1[1]
capacity = len(test1)
index1 = 0
while index1 != capacity:
line = test1[index1]
for t1Out in line.split():
print(t1Out)
index1 = index1 + 1
# ================================================
with open("grades.txt", "r") as f:
for line in f:
test2.append(line.split(",", -1)[2])
line = test2[2]
capacity = len(test2)
index2 = 0
while index2 != capacity:
line = test2[index2]
for t2Out in line.split():
print(t2Out)
index2 = index2 + 1
# ================================================
with open("grades.txt", "r") as f:
for line in f:
test3.append(line.split(" ", -1)[3])
line = test3[3]
capacity = len(test3)
index3 = 0
while index != capacity:
line = test3[index3]
for t3Out in line.split():
print(t3Out)
index3 = index3 + 1
# ==============================================================================
def main():
fn = "grades.txt" # set the working file name
scores = 3 # set the number of scores
getStudentData(fn, scores) # Calls getStudentData()
line = getTextFileContents(fn) # Assigns getTextFileContent() to line
printReport(line) # Calls printReport()
main()
Check the lines 检查线
line = test1[1]
line = test2[2]
line = test3[3]
You should start the list from 0
, not 1
, 2
, 3
你应该从启动列表中
0
,而不是1
, 2
, 3
The offending section is this: 令人讨厌的部分是这样的:
while index != capacity:
line = test3[index3]
for t3Out in line.split():
print(t3Out)
index3 = index3 + 1
The first line should be: 第一行应为:
while index3 != capacity:
Note that manually incrementing an index is probably not the best way to loop over a list in Python. 请注意,手动增加索引可能不是在Python中遍历列表的最佳方法。 A more pythonic way might be something like:
更加Python化的方式可能类似于:
for item in test3:
for t3out in item.split():
print(t3out)
Also, if you're going to stick with incrementing manually, you need to remove the index = index + 1
from inside the for loop. 另外,如果您要坚持手动递增,则需要从for循环中删除
index = index + 1
。 You should only increment after processing each line. 您只应在处理完每一行后递增。
while index != capacity:
line = test3[index3]
for t3Out in line.split():
print(t3Out)
index3 = index3 + 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.