gpt4 book ai didi

python - 我如何修复这个 "ValueError: can' t have unbuffered text I/O"in python 3?

转载 作者:太空狗 更新时间:2023-10-29 20:46:54 24 4
gpt4 key购买 nike

这是麻省理工学院的python项目问题之一,但它基本上是为python 2.x用户编写的,那么有什么办法可以修复以下代码以在最新的python 3中运行?

当前代码引发“ValueError: can't have unbuffered text I/O”

WORDLIST_FILENAME = "words.txt"

def load_words():

print("Loading word list from file...")

inFile = open(WORDLIST_FILENAME, 'r', 0)
# wordlist: list of strings
wordlist = []
for line in inFile:
wordlist.append(line.strip().lower())
print(" ", len(wordlist), "words loaded.")
return wordlist

最佳答案

来自 open 的文档字符串:

... buffering is an optional integer used to set the buffering policy. Pass 0 to switch buffering off (only allowed in binary mode) ...

所以改变 inFile = open(WORDLIST_FILENAME, 'r', 0)

inFile = open(WORDLIST_FILENAME, 'r'),或到

inFile = open(WORDLIST_FILENAME, 'rb', 0) 如果您真的需要它(我对此表示怀疑)。

关于python - 我如何修复这个 "ValueError: can' t have unbuffered text I/O"in python 3?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45263064/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com