我一直在尝试解析一个数据文件并因此创建了这个类,我遇到的问题是在将我的代码放入一个类之前它似乎工作正常。但是因为我想在代码中进行即兴创作以使其更有效率,我想要定义一个类。这是我的代码在我放入我的课程之前工作正常并且还缩进了#34;。 / p>
class Athelete:
# init function to create object instances.
def __init__(self, a_name, a_dob, a_times=[]):
self.name = a_name
self.dob = a_dob
self.times = a_times
# top3 function to display the 3 best times of my athelete times attribute
def top3(self):
return(sorted(set(sanitize(times)))[0:3])
# main_parser is reponsible for converting each line in the file
#to a list using ",(comma)" as the identifier to split.
def main_parser(self, filename):
try:
with open(filename + ".txt", 'r') as james_list:
for each_line in james_list:
templ = each_line.strip().split(',')
return(Athelete(templ.pop(0), templ.pop(0), templ)
except IOError as err:
print("File Error: " + str(err))
return(None)
#Sanitize function removes the '-(dash)',':(colon)' from the list and #replaces
#them with a 'period' so that using sorted BIF becomes possible
def sanitize(self, time_list):
sanitized=[]#a new list to store the end result
for each_element in time_list:
if '-' in each_element:
splitter='-'
elif ':' in each_element:
splitter=':'
else:
splitter='.'
(mins, secs)=each_element.split(splitter)
#using split to remove the '-',':' '''
sanitized.append(mins + '.' + secs)
#then joining the items from lists mins and secs
return(sanitized)
#returning the sanitized list that is without the unwanted bits.
我在"得到错误,除了IOError为错误"行说无效语法。 如果有任何意义,我正在使用默认IDLE ..
答案 0 :(得分:1)
您在该行上缺少右括号:
return(Athelete(templ.pop(0), templ.pop(0), templ)
^