鱼C论坛

 找回密码
 立即注册
查看: 1189|回复: 6

关于文件数据处理问题

[复制链接]
发表于 2019-6-30 17:37:00 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. # -*- coding:utf8 -*-

  2. class AthleteList(list):

  3.     """
  4.     初始化类属性
  5.     name:运动员姓名
  6.     birth:运动员出生日期
  7.     time:时间成绩
  8.     """
  9.     def __init__(self, name, birth=None, times=[]):
  10.         list.__init__([])
  11.         self.name = name
  12.         self.birth = birth
  13.         self.times = times
  14.         self.extend(times)

  15.     def santize(self):
  16.         if "-" in self:
  17.             splitter = "-"
  18.         if ":" in self:
  19.             splitter = ":"
  20.         else:
  21.             return self
  22.         (mins, secs) = self.split(splitter)
  23.         return mins + "." + secs

  24.     def top3(self):
  25.         return print(sorted(set([self.santize(t) for t in self]))[0:3])

  26. def get_coach_data(filename):
  27.     try:
  28.         with open(filename, "r") as file:
  29.             data = file.read()
  30.     except IOError as file_err:
  31.         print("{0}".format(file_err))
  32.     else:
  33.         tmp = data.strip().split(",")
  34.         return AthleteList(tmp.pop(0), tmp.pop(0),tmp)

  35. if __name__ == "__main__":
  36.     james = get_coach_data("james2.txt")
  37.     james.top3()
复制代码


求问各位大佬,为什么会报错:
  1. D:\python练习\venv\Scripts\python.exe D:/python练习/Head_First_Python/第六章_定制数据_打包代码和数据/AthleteList.py
  2. Traceback (most recent call last):
  3.   File "D:/python练习/Head_First_Python/第六章_定制数据_打包代码和数据/AthleteList.py", line 43, in <module>
  4.     james.top3()
  5.   File "D:/python练习/Head_First_Python/第六章_定制数据_打包代码和数据/AthleteList.py", line 29, in top3
  6.     return print(sorted(set([self.santize(t) for t in self]))[0:3])
  7.   File "D:/python练习/Head_First_Python/第六章_定制数据_打包代码和数据/AthleteList.py", line 29, in <listcomp>
  8.     return print(sorted(set([self.santize(t) for t in self]))[0:3])
  9. TypeError: santize() takes 1 positional argument but 2 were given
复制代码


我传入sanitize的参数明明是一个数据啊,为什么报错会是两个
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-6-30 17:42:07 | 显示全部楼层
head first

我没看错的话应该是[self.santize(t) for t in self.times]  #少了.times
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-6-30 17:44:04 | 显示全部楼层
newu 发表于 2019-6-30 17:42
head first

我没看错的话应该是[self.santize(t) for t in self.times]  #少了.times

哪里少了.times?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-6-30 17:44:51 | 显示全部楼层

就是报错的那一行,第29行
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-6-30 17:47:42 | 显示全部楼层
newu 发表于 2019-6-30 17:44
就是报错的那一行,第29行

尝试了下,不对 依然提示多提供了一个参数,我print了下for t in self.times: print(t),结果确实只有一个参数
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-6-30 18:10:34 | 显示全部楼层
知道了,在sanitize方法定义的时候,少定义了个参数,所以在return print(sorted(set([self.santize(t) for t in self]))[0:3])调用的时候,报错多传了个参数,因为本身self是一个参数,但是利用for循环又传了个参数 所以报错
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-6-30 18:26:33 | 显示全部楼层
迷茫的兔子 发表于 2019-6-30 18:10
知道了,在sanitize方法定义的时候,少定义了个参数,所以在return print(sorted(set([self.santize(t) for ...

好的
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2026-1-16 15:20

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表