我一直在努力打造个人组织者。每天都会呈现为一个对象,其中包含在几个给定活动上花费的小时数。理想情况下,它还允许用户将此信息保存到文件并从以前的条目导入。
以下是代码:
class Day:
def __init__(self, date, hours, python, math, story, phil, medi):
self.date = int(date)
self.hours = float(hours)
self.python = float(python)
self.math = float(math)
self.story = float(story)
self.phil = float(phil)
self.medi = float(medi)
def getDay(self):
return repr((self.date, self.hours, self.python, self.math, self.story, self.phil, self.medi))
def getDayList(self):
return list(self.date, self.hours, self.python, self.math, self.story, self.phil, self.medi)
def getInfo():
#the following allows the user to import an existing file with one object per line
question = input("Enter new information or import an existing file(new/imp)")
if question[0] == "i" or question[0] == "I":
fileName = input("What is the name of the file you would like to import?")
inFile = open(fileName, "r")
dayList = []
for line in inFile:
dayList.append(line)
print(dayList)
return dayList
inFile.close()
#the following allows the user to write objects to a new file or append objects to an existing file
#though I have not tested the latter feature
elif question[0] == "n" or question[0] == "N":
question2 = input("Would you like to append this new info to an existing file or write it to a new file(a/w)? Hit blank ente to exit")
fileName = input("Enter filename for new information")
outFile = open(fileName, question2)
dayList = []
while question2 != "":
date = input("Please enter date (in single string)")
hours = input("please enter total hours for the day")
python = input("please enter total hours of python")
math = input("please enter total hours of math")
story = input("please enter total hours of story writing")
phil = input("please enter total hours of philosophy")
medi = input("please enter total hours of meditation")
#creates object
dayObject = Day(date, hours, python, math, story, phil, medi)
dayList.append(dayObject)
question2 = input("Would you ;like to enter more info now (enter 'yes' or hit blank enter to conclude)")
#writes each object to the file
for item in dayList:
print(item, file=outFile)
return dayList
outFile.close()
def main():
dayList = getInfo()
test = dayList[0]
print(test)
print(test.getDay())
main()
当我创建一个新文件时,我可以在对象上使用方法:getDay(),例如,返回类的各个方面(日期,小时,python等)。但是,当我使用getInfo()函数的前半部分中的代码导入相同的文件时,我收到此错误消息:
Traceback (most recent call last):
File "/Users/rainydaycart/Documents/po2.py", line 101, in <module>
main()
File "/Users/rainydaycart/Documents/po2.py", line 98, in main
print(test.getDay())
AttributeError: 'str' object has no attribute 'getDay'
当我打印新制作的对象时,它似乎是一个对象而不是一个字符串。当我从文件中导入相同的对象时,为什么我不能执行getDay()方法,这是类定义的一部分?
P.S。使用函数头(模块?)在这篇文章中缩进似乎有点screwey,但是当我运行文件时它们不是问题。
答案 0 :(得分:0)
当你做某事 -
for item in dayList:
print(item, file=outFile)
你实际上并没有写对象,你只是为它写str(object)
,而在你的情况下,由于你的班级没有定义__str__()
或__repr__()
,它会是写作 -
<__main__.Day object at 0x00260F10>
显示此内容的简单示例 -
>>> class CA:
... pass
...
>>> with open('a.txt','w') as f:
... print(c,file=f)
...
最后的文件包含 -
<__main__.CA object at 0x00260F10>
您实际想要使用的是 - pickle
模块,用于对象序列化和反序列化。
然后,您可以使用pickle.dump()
将对象写入文件,并使用 - pickle.load()
加载。
示例 -
>>> class CA:
... pass
...
>>> c = CA()
>>> c.x = 10
>>> import pickle
>>> with open('a.txt','wb') as f:
... pickle.dump(c,f)
...
>>> del c
>>> with open('a.txt','rb') as f:
... d = pickle.load(f)
...
>>> d
<__main__.CA object at 0x007150F0>
>>> d.x
10
答案 1 :(得分:0)
在main()
中,不应该是:
def main():
object = Day()
dayList = object.getInfo()
test = dayList[0]
此外,您的outFile.close()
和inFile.close()
没有被击中,因为它们位于return()
之后。