io.UnsupportedOpperation: 阅读
io.UnsupportedOpperation: read
我正在编写一个程序来获取用户输入并确定用户输入是否在列表 'yes' 或 'no' 中。我想使用 pickle 来教我的程序新形式的是或否答案,方法是在代码看到新答案时询问用户它是 'yes' 还是 'no' 类型的答案。但是,当我尝试打开包含列表的文件时出现错误。这是我的代码:
import pickle
with open('yes.pkl', 'wb') as f:
yes = pickle.load(f)
with open('no.pkl', 'wb') as f:
no = pickle.load(f)
no = ["no", "never", "why should i", "nope", "noo", "nop", "n", "why", "no way", "not in a million years"]
yes = ["yes", "okay", "sure", "why not", "fine", "yah", "yeah", "y", "yee", "yesh", "yess", "yup", "yeppers", "yupperdoodle", "you bet"]
def closedq(x):
if x in no:
print("Meany.")
quit()
if x in yes:
print()
else:
time.sleep(1)
print()
print("I have not yet learned that term.")
time.sleep(1)
print("Is this a yes, or a no answer?")
yesno = input()
if yesno in yes:
yes.append(x)
with open('yes.pkl', 'wb') as f:
pickle.dump(yes, f)
if yesno in no:
no.append(x)
with open('no.pkl', 'wb') as f:
pickle.dump(no, f)
else:
print("Meany.")
quit()
print("Thank you for your input. ")
print()
print()
time.sleep(1)
print("Do you want to play a game?")
print()
play = input()
closedq(play)
print("Yay!")
我一直收到的错误如下。
Traceback (most recent call last):
File "main.py", line 3, in <module>
yes = pickle.load(f)
io.UnsupportedOperation: read
我做错了什么?
您的代码正在以 write-only 模式打开文件,因此从中读取以加载已腌制的数据会失败。问题在于此行(以及 no
的等效行):\
with open('yes.pkl', 'wb') as f:
要读取文件,您需要模式 'rb'
,而不是 'wb'
。在代码的更下方,当您写入文件时,您会正确打开以进行写入,但您不希望它位于顶部。
请注意,当文件尚不存在时,您可能需要在代码中添加额外的逻辑。以写入模式打开 non-existent 文件没问题,您只需创建它即可。但在读取模式下,文件需要已经存在。如果文件尚不存在,将 yes
和 no
初始化为空列表可能是您想要的,但我尚未完全检查您的逻辑以了解这是否是最佳方法。
我正在编写一个程序来获取用户输入并确定用户输入是否在列表 'yes' 或 'no' 中。我想使用 pickle 来教我的程序新形式的是或否答案,方法是在代码看到新答案时询问用户它是 'yes' 还是 'no' 类型的答案。但是,当我尝试打开包含列表的文件时出现错误。这是我的代码:
import pickle
with open('yes.pkl', 'wb') as f:
yes = pickle.load(f)
with open('no.pkl', 'wb') as f:
no = pickle.load(f)
no = ["no", "never", "why should i", "nope", "noo", "nop", "n", "why", "no way", "not in a million years"]
yes = ["yes", "okay", "sure", "why not", "fine", "yah", "yeah", "y", "yee", "yesh", "yess", "yup", "yeppers", "yupperdoodle", "you bet"]
def closedq(x):
if x in no:
print("Meany.")
quit()
if x in yes:
print()
else:
time.sleep(1)
print()
print("I have not yet learned that term.")
time.sleep(1)
print("Is this a yes, or a no answer?")
yesno = input()
if yesno in yes:
yes.append(x)
with open('yes.pkl', 'wb') as f:
pickle.dump(yes, f)
if yesno in no:
no.append(x)
with open('no.pkl', 'wb') as f:
pickle.dump(no, f)
else:
print("Meany.")
quit()
print("Thank you for your input. ")
print()
print()
time.sleep(1)
print("Do you want to play a game?")
print()
play = input()
closedq(play)
print("Yay!")
我一直收到的错误如下。
Traceback (most recent call last):
File "main.py", line 3, in <module>
yes = pickle.load(f)
io.UnsupportedOperation: read
我做错了什么?
您的代码正在以 write-only 模式打开文件,因此从中读取以加载已腌制的数据会失败。问题在于此行(以及 no
的等效行):\
with open('yes.pkl', 'wb') as f:
要读取文件,您需要模式 'rb'
,而不是 'wb'
。在代码的更下方,当您写入文件时,您会正确打开以进行写入,但您不希望它位于顶部。
请注意,当文件尚不存在时,您可能需要在代码中添加额外的逻辑。以写入模式打开 non-existent 文件没问题,您只需创建它即可。但在读取模式下,文件需要已经存在。如果文件尚不存在,将 yes
和 no
初始化为空列表可能是您想要的,但我尚未完全检查您的逻辑以了解这是否是最佳方法。