我已经做了这个函数,它允许我输入一个引用和一个作者,然后将它们pickle到一个.txt文件中。你知道吗
然而,似乎我可能被限制为只有一行酸洗信息,当我执行“enterquote”函数时,我最终会覆盖这些信息。我似乎不知道如何把更多的信息放进文件里。你知道吗
我怎么能这么做?你知道吗
def readquote():
f = open("quote.txt","r")
pquote = pickle.load(f)
pauthor = pickle.load(f)
print "%3s\n - %s" % (pquote, pauthor)
def enterquote():
f = open("quote.txt","w")
quote = raw_input("What quote has its place in the quote book? \n to quit press Q\n\n")
if quote != "Q":
pickle.dump(quote, f)
author = raw_input("what author said that? \n to quit press Q \n\n")
pickle.dump(author, f)
if author == "Q":
print "goodbye"
elif quote == "Q":
print "goodbye"
f.close()
我用架子试过这个:
def readquote():
f = shelve.open('quote.txt')
psaying = f[quote["saying"]]
pauthor = f[quote["author"]]
for key in quote :
print "%3s\n - %s" % (psaying, pauthor)
def enterquote2():
f = shelve.open('quote.txt')
quote = {}
quote["saying"] = raw_input("What quote has its place in the quote book? \n to quit press Q\n\n")
if quote != "Q":
f['saying'] = saying
quote[author] = raw_input("what author said that? \n to quit press Q \n\n")
f['author'] = author
if author == "Q":
print "goodbye"
elif saying == "Q":
print "goodbye"
f.close()
但是它给了我一个关于“psaying=f[quote[“saying”]”的错误
我想用词汇格式把说辞和作者结合成引语,这样我就可以把它们一个接一个地放在书架上,但这让我很困惑。你知道吗
酸洗是针对单个对象的。如果要持久保存多个对象,请使用
shelve
模块。它将为您提供一个键值存储。你知道吗例如,可以将
enterquote
更改为以下内容:然后还需要以类似的方式修改
readquote
函数。你知道吗相关问题 更多 >
编程相关推荐