程序DOES迭代,但只到某一行代码。然后它开始读取开始和停止变量,如7和9?!?!我不明白这里有什么问题。更奇怪的是,它跳跃到7,9到48,51(开始,停止值)。 以下是代码的主要部分:http://pastebin.com/S0FZ3Jk7 这是我正在使用的数据:http://pastebin.com/rchNJGBq
"""
Qualifiers_2.py
"""
#from Qualifier_classes import Qualifier
file_path = 'C:\\Users\\Neo\\My Documents\\Python Scripts\\FTC Scouting\\sample.txt'
file = open(file_path, 'r')
Data = []
all_teams = []
Teams_list = []
keys = ['Team Number: ', 'Name: ','Qualifier: ']
qualifier_keys = ['Qualifier: ', 'QP: ', 'RP: ', 'HS: ', 'Matches: ']
team_attr = ['name','number']
UI_options = [1,2]
class Qualifier(object):
def __init__(self):
self.name = 'Qualifier Name'
self.rp = 0
self.qp = 0
self.hs = 0
self.num_of_matches = 0
self.data = [self.name,self.rp,self.qp,self.hs,self.num_of_matches]
def __repr__(self):
self.data = [self.name,self.rp,self.qp,self.hs,self.num_of_matches]
return repr((self.data))
class Team:
def __init__(self,name,number):
self.name = name
self.number = number
def __repr__(self):
return repr((self.name,self.number))
def list_teams(n):
tTeams = []
for line in Data:
check = line.find(keys[n])
if not check == -1:
team = line.partition(keys[n])[2]
if team not in tTeams:
tTeams.append(team)
return tTeams
#@param: team_number , team to find
#@output: gives back line number in raw Data list
def find_start_team(team_number):
tcount = 0
if str(team_number) in list_teams(0):
for line in Data:
if not line.find(str(team_number)) == -1:
return tcount
else:
tcount += 1
else:
return 'not a valid team number'
#@param: Start_team_number , the previous team next number
#@output: the line number for the start and end of the team's acquired info
def find_team(start_team_number):
try:
start_search = find_start_team(start_team_number)
tcount = start_search
for line in Data[start_search+1:]:
if not line.find(keys[0]) == -1:
return start_search, tcount
else:
tcount += 1
except ValueError:
return 'not a team number'
def team_data(start,stop):
temp_data = []
for line in Data[start:stop]:
temp_data.append(line)
return temp_data
#@param: qualifier_key , the name of the category you wish to find
#@param: data , the qualifier info for some team
#@output: returns back the data for the category
def get_data(qualifier_key, data):
for line in data:
if not line.find(qualifier_key) == -1:
return line.partition(qualifier_key)[2]
#@param: data , the qualifier data for some team
#@output: remaining_data , the remaining data after the first qualifier
# note: will return -1 if there is no more qualifiers
def just_qualifiers(data):
tcount = 0
for line in data:
if not line.find(qualifier_keys[0]) == -1:
return data[tcount:]
tcount += 1
return -1
#@param: data , array of values for a single qualifier
def update_qualifier(data):
tqual = Qualifier()
tqual.name = data[0]
tqual.rp = data[1]
tqual.hs = data[2]
tqual.num_of_matches = tdata[3]
return tqual
#@param: data , the block of data for the team
#@output: the qualifier data and remainind data
def get_qualifier(data):
temp_data = []
for key in range(len(qualifier_keys)):
temp_data.append(get_data(qualifier_keys[key],data))
temp_qual = update_qualifier(temp_data)
data = data[len(temp_data):]
return temp_qual, data
#Turns our txt file into usable data
for line in file:
line = line[:-1]
Data.append(line)
#Creates a tuple for holding the ID of all the teams
all_teams.append(list_teams(0))
all_teams.append(list_teams(1))
#Makes an organized list of the ID for all the teams in a 2-tuple
for team in range(len(list_teams(0))):
Teams_list.append(Team(all_teams[1][team],int(all_teams[0][team])))
"""*******************
NEW CODE
*******************"""
Qualifiers = []
for team in range(len(all_teams[0])):
start , stop = find_team((all_teams[0][team]))
tdata = []
tqual = []
#maps the specific teams block of data to tdata
for line in team_data(start, stop):
tdata.append(line)
tdata = just_qualifiers(tdata)
while True:
if not just_qualifiers(tdata) == -1:
tqual , tdata = get_qualifier(tdata)
Qualifiers.append(tqual)
else:
break
print Qualifiers
"""*******************
END NEW CODE
*******************"""
file.close()
和我的TraceBack:
Traceback (most recent call last):
File "C:/Users/Neo/Documents/Python Scripts/FTC Scouting/Qualifiers_3.py", line 152, in <module>
if not just_qualifiers(tdata) == -1:
File "C:/Users/Neo/Documents/Python Scripts/FTC Scouting/Qualifiers_3.py", line 96, in just_qualifiers
for line in data:
TypeError: 'int' object is not iterable
答案 0 :(得分:0)
我不会检查你的整个程序来解决这个问题,但我确实抓住了一件事,作为追溯的一部分:
def update_qualifier(data):
tqual = Qualifier()
tqual.name = data[0]
tqual.rp = data[1]
tqual.hs = data[2]
tqual.num_of_matches = tdata[3]
return tqual
您的意思是在这里引用全局变量tdata[3]
,而不是您的本地data
吗?
答案 1 :(得分:0)
您的just_qualifiers
方法返回列表或-1(yuk!为什么不是空列表?)。
你调用它一次,获取值-1,将其分配给tdata,然后立即调用just_qualifiers
AGAIN(为什么?),如下所示:if not just_qualifiers(tdata) == -1:
相当于if not just_qualifiers(-1) == -1:
导致函数内部出现观察到的错误 - 它正在尝试for line in -1:
我强烈建议您重写该函数,以便在没有限定符的情况下返回空列表。同样任何其他类似的功能。迭代空列表是非常安全的,即如果for line in data:
为空,data
不会优雅地执行任何操作。如果您需要检查它是否为空,您可以根据需要执行if data:
或if not data:
。