-3

该程序确实会迭代,但只会迭代到某一行代码。然后它开始将开始和停止变量读取为 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 (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
4

2 回答 2

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]这里引用一个全局变量,而不是你的 localdata吗?

于 2012-04-09T03:09:18.583 回答
0

您的just_qualifiers方法返回一个列表或 -1(哎呀!为什么不是一个空列表?)。

您调用它一次,获取值 -1,将其分配给 tdata,然后立即调用just_qualifiersAGAIN(为什么?),如下所示: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:根据需要。

于 2012-04-09T05:38:59.993 回答