'Python Read a list from a file and assign to a new list
I have a text file that contains a list This is what the list in the file like
[['1', 'a', 66, 66, 66, 66], ['1', 'b', 77, 77, 77, 77], ['1', 'c', 77, 88, 88, 88], ['1', '', 99, 99, 99, 99], ['3', 'd', 76, 87, 98, 78], ['3', 'e', 67, 90, 76, 89], ['3', 'f', 77, 99, 77, 66], ['3', 'g', 77, 88, 99, 77]]
I do have one that works that does not read the file it just iterates through a list
i=0
j=0
k=0
m=0
n=0
p=0
total=0
TeamInfo = []
TeamScores = []
while i < 2:
TeamName = input("Enter Team Name:")
while j < 4:
TeamMember = (input("Enter Player's Name:"))
j+=1
TeamInfo.append([TeamName, TeamMember])
j = 0
i+=1
while k < 8:
while m < 4:
getScore = "Please Enter Score # "+str(m+1)+" for team member " +str(TeamInfo[k][1])+":"
Score = int(input(getScore))
while Score < 65 or Score > 115:
Score = int(input(getScore))
TeamInfo[k].append(Score)
m+=1
m = 0
k+=1
try:
with open("Golf_Team_Info.txt", "a") as w:
w.write("%s\n" %TeamInfo)
w.close
except:
print("File does not exist")
for n in range(len(TeamInfo)):
total = sum(filter(lambda j: isinstance(j, int),TeamInfo[n]))
avg = total/4
Name = str(TeamInfo[n][1])
TeamScores.append([Name, total])
print("Score for Team Member:", TeamInfo[n][1], "on Team", TeamInfo[n][0], "is",total, "with a avegrage score of", avg)
r = open("Golf_Team_Info.txt", "r")
print(r.read())
for p in range(len(TeamScores)):
if p == 0:
lowest = TeamScores[0][1]
winner = TeamScores[0][0]
elif lowest > TeamScores[p][1]:
lowest = TeamScores[p][1]
winner = TeamScores[p][0]
print("The player with the lowest score is" ,winner,"with a score of",lowest)
When I use the read file I would like to go through the list and read the elements in it but when I do it doesn't comes up as a list and when I try to read an item in the element I get a index out of range
p = "Golf_Team_Info.txt"
n = 0
j = 0
TeamInfo = []
with open(p,'r+') as scores:
test = scores.readline()
TeamInfo = test
for n in range(len(TeamInfo)):
total = sum(filter(lambda j: isinstance(j, int),TeamInfo[n]))
avg = total/4
Name = str(TeamInfo[n][1])
TeamScores.append([Name, total])
print("Score for Team Member:", TeamInfo[n][1], "on Team", TeamInfo[n][0], "is",total, "with a avegrage score of", avg)
Solution 1:[1]
Thank you Mark Tolonen for pointing me to ast.literal_eval Got my code working now
import ast
r = "Golf_Team_Info.txt"
n = 0
p = 0
TeamScores = []
with open(r,'r+') as scores:
test = scores.readline()
TeamInfo = ast.literal_eval(test)
for n in range(len(TeamInfo)):
total = sum(filter(lambda j: isinstance(j, int),TeamInfo[n]))
avg = total/4
Name = str(TeamInfo[n][1])
TeamScores.append([Name, total])
print("Score for Team Member:", TeamInfo[n][1], "on Team", TeamInfo[n][0], "is",total, "with a avegrage score of", avg)
for p in range(len(TeamScores)):
if p == 0:
lowest = TeamScores[0][1]
winner = TeamScores[0][0]
elif lowest > TeamScores[p][1]:
lowest = TeamScores[p][1]
winner = TeamScores[p][0]
print("The player with the lowest score is" ,winner,"with a score of",lowest)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Jose Ortiz |
