Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python类文件中的int和string错误_Python_String_Class_Python 3.x_Methods - Fatal编程技术网

Python类文件中的int和string错误

Python类文件中的int和string错误,python,string,class,python-3.x,methods,Python,String,Class,Python 3.x,Methods,我正在编写一个python程序,其中有3个文件。一个是主文件,一个是类文件,一个是数据文件。数据文件从2个文本文件中读取数据,并拆分和排列数据,以供类和主文件使用。无论如何,我对数据和主文件已经做了很多,但是我对类文件有问题。这是一个一般的字符串格式问题,但我不明白我可以做什么来修复它。我发现了错误 “File”/Users/admin/Desktop/Program 6/FINAL/classFile.py”,第83行, 在报告中 如果len(self._birth[0])我觉得birth是一

我正在编写一个python程序,其中有3个文件。一个是主文件,一个是类文件,一个是数据文件。数据文件从2个文本文件中读取数据,并拆分和排列数据,以供类和主文件使用。无论如何,我对数据和主文件已经做了很多,但是我对类文件有问题。这是一个一般的字符串格式问题,但我不明白我可以做什么来修复它。我发现了错误

“File”/Users/admin/Desktop/Program 6/FINAL/classFile.py”,第83行, 在报告中
如果len(self._birth[0])我觉得
birth
是一个整数列表,而不是字符串列表

如果要确保它们都是字符串,可以尝试:

self._birthDay = list(map(str, birthDay))
或者,如果您知道它们都是字符串,您可以首先使用字符串格式来避免这些
len
检查:

self._birthDay = ['{:02d}'.format(x) for x in birthDay]
不过,更好的方法是将
生日
表示为
日期时间.datetime
对象。假设它总是以3整数、月、日、年的形式出现,您可以:

bmon, bday, byear = birthDay
self._birthDay = datetime.datetime(byear, bmon, bday)
然后您的
\uuuu repr\uuuu
可以使用该方法

编辑

为了响应您的更新,我认为您应该将
from datetime import datetime
添加到
getData
的顶部,然后使用以下命令而不是解析月/日/年:

birthDay = datetime.strptime(x[3], '%m/%d/%Y')
这将为您提供一个完整的
datetime
对象来表示生日(或者,您可以使用
datetime.date
对象,因为您不需要时间)

然后,您可以用以下方法替换
\uuuu repr\uuu
方法:

def __repr__(self):
    fmtstr = '{first} {last} (#{techid})\nAge: {age} ({bday})\nGPA: {gpa:0.2f} ({credits})'
    bday = self._birthDay.strftime('%m/%d/%Y')
    return fmtstr.format(first=self._first,
                         last=self._last,
                         age=self.currentAge(),
                         bday=bday,
                         gpa=self.currentGPA(),
                         credits=self._totalCredits)

哦,由于
\u birth
现在是
datetime.datetime
,您需要更新
currentAge()
以返回
int((datetime.datetime.now()-self.\u birth)/datetime.timedelta(days=365))
这将相当准确,不会太复杂。

在我看来,生日是一个整数列表,而不是字符串列表

如果要确保它们都是字符串,可以尝试:

self._birthDay = list(map(str, birthDay))
或者,如果您知道它们都是字符串,您可以首先使用字符串格式来避免这些
len
检查:

self._birthDay = ['{:02d}'.format(x) for x in birthDay]
不过,更好的方法是将
生日
表示为
日期时间.datetime
对象。假设它总是以3整数、月、日、年的形式出现,您可以:

bmon, bday, byear = birthDay
self._birthDay = datetime.datetime(byear, bmon, bday)
然后您的
\uuuu repr\uuuu
可以使用该方法

编辑

为了响应您的更新,我认为您应该将
from datetime import datetime
添加到
getData
的顶部,然后使用以下命令而不是解析月/日/年:

birthDay = datetime.strptime(x[3], '%m/%d/%Y')
这将为您提供一个完整的
datetime
对象来表示生日(或者,您可以使用
datetime.date
对象,因为您不需要时间)

然后,您可以用以下方法替换
\uuuu repr\uuu
方法:

def __repr__(self):
    fmtstr = '{first} {last} (#{techid})\nAge: {age} ({bday})\nGPA: {gpa:0.2f} ({credits})'
    bday = self._birthDay.strftime('%m/%d/%Y')
    return fmtstr.format(first=self._first,
                         last=self._last,
                         age=self.currentAge(),
                         bday=bday,
                         gpa=self.currentGPA(),
                         credits=self._totalCredits)
哦,由于
\u birth
现在是
datetime.datetime
,您需要更新
currentAge()
以返回
int((datetime.datetime.now()-self.\u birth)/datetime.timedelta(days=365))
这将在不太复杂的情况下相当准确。

使用,而不是字符串串联,它更干净:

另外,如果您使用此格式,它将自动转换您使用的类型,而不是字符串连接,它更干净:


另外,如果您使用此格式,它将自动为您转换类型,因为错误消息指出,
len
对于int没有意义。如果您需要其中的字符数,请先将其转换为str

def __repr__(self):
    if len(str(self._birthDay[0]))<2:
        self._birthDay[0] = "0" + str(self._birthDay[0])
    elif len(str(self._birthDay[1]))<2:
        self._birthDay[1] = "0" + str(self._birthDay[1])
    return self._first + " " + self._last + " (#" + self._techID + ")\nAge: " + str(self.currentAge()) + \
           " (" + str(self._birthDay[0]) + "/" + str(self._birthDay[1]) + "/" + str(self._birthDay[2]) + ")" + \
           "\nGPA: %0.2f" % (self.currentGPA()) + " (" + str(self._totalCredits) + " Credits" + ")\n"
def\uuu repr\uu(自):

如果len(str(self._birth[0])如错误消息所述,
len
对于int没有意义。如果需要其中的字符数,请先将其转换为str

def __repr__(self):
    if len(str(self._birthDay[0]))<2:
        self._birthDay[0] = "0" + str(self._birthDay[0])
    elif len(str(self._birthDay[1]))<2:
        self._birthDay[1] = "0" + str(self._birthDay[1])
    return self._first + " " + self._last + " (#" + self._techID + ")\nAge: " + str(self.currentAge()) + \
           " (" + str(self._birthDay[0]) + "/" + str(self._birthDay[1]) + "/" + str(self._birthDay[2]) + ")" + \
           "\nGPA: %0.2f" % (self.currentGPA()) + " (" + str(self._totalCredits) + " Credits" + ")\n"
def\uuu repr\uu(自):


如果len(str(self.\u生日[0]))
self.\u birth
是一个字符串吗?你什么时候会遇到这个错误?你应该避免在
\uuuu repr\uuuu
方法中修改对象,而不是更新一些缓存或其他东西。为了更清楚起见,我已经更新并添加了我的主文件和数据文件。
self.\u birth
是一个字符串吗?你什么时候会遇到这个错误?你应该ld可能避免在
\uuu repr\uuuu
方法中修改对象,而不是更新某些缓存或其他内容。为了更清晰,我更新并添加了主文件和数据文件。对此表示抱歉。为了更清晰,我现在添加了主文件和数据文件。@JakeMeyer更新了我的答案。谢谢,但如果我使用此方法,我将如何更新def currentAge(自身)中的e if else语句方法?如果myMonth
datetime
对象具有
year
month
day
属性(如果您愿意),但是如果您只关心是否合理准确,您可以使用我在答案末尾建议的一行代码替换整个方法。更清楚的是,您可以执行
today=datetime.today()方法?如果myMonth
datetime
对象具有
year
month
day
属性(如果您愿意),但是如果您只关心是否合理准确,您可以使用我在答案末尾建议的一行代码替换整个方法。更清楚的是,您可以执行
today=datetime.today()
,然后是
self.\u birth.month很抱歉。为了更清晰,我现在添加了主文件和数据文件。它给了我错误文件“/Users/admin/Desktop/Program 6/FINAL/classFile.py”,第84行,在repr self中。\u birth[0]=“0”+str(self.\u birth[0])类型