gpt4 book ai didi

python - UnicodeDecodeError : 'ascii' codec can't decode byte 0xff in position 36: ordinal not in range(128)

转载 作者:行者123 更新时间:2023-12-03 08:48:38 25 4
gpt4 key购买 nike

我正在尝试运行一个程序,该程序基于文本文件的内容管理测验。我的代码如下所示:

该程序管理有关美国各州,首都和州长的测验

全局有多少个问题

HOW_MANY_QUESTIONS = 10

打开带有答案的文件

answer_file = open('quiz_answers.txt','r')

打开文件以在末尾添加结果

results_file = open('quiz_results.txt','w')

def main():

#dictionary with quiz answers
answer_dictionary={}

#run create dictionary function, result is the answer dictionary
answer_dictionary=create_dictionary(answers_file)

#close the files
answers_file.close()
results_file.close()

go_again='yes'

while go_again=='yes':

#get name
name=input('Enter your name: ')

#initialize count variable
count=0

#accumulator for number of correct answers
number_correct=0

for count in range(0,HOW_MANY_QUESTIONS+1):


#pick question function
answer=pick_question(answer_dictionary)

if answer:

number_correct+=1

results_file.write(name,number_correct)



go_again=input('Go again? ')

def create_dictionary(infile):
#establish a dictionary 
dictionary={}

#initialize a key
key=0

#read the first line that is a state
state=infile.readline()

#strip the new line
state=state.rstrip('\n')

#while the line is not blank
while state!='':

#add it as the dictionary key
key=state

#read the capital line
capital=infile.readline()

#strip new line
capital=capital.rstrip('\n')

#read the govenror line
governor=infile.readline()

governor=governor.rstrip('\n')

#assign a list with capital and governor to the state key
dictionary[key]=[capital,governor]

return dictionary

def pick_question(字典):
#make a list containing the keys
keys=[]

#isolate the keys and append to the key list
for key in dictionary.keys():
keys.append(key)

#import random
import random

#initialize an index for keys list
key_index=0

#randomly select the state to ask about
state_selection=random.randint(0,len(keys)-1)


key_index=state_selection

#assign value to capital and governor to randomly
#select which question to ask
capital=1
governor=2


#randomly select whether to ask for the governor or the capital
question_choice=random.randint(capital,governor)

#if the question choice is for capital
if question_choice==capital:

#ask the capital question
question=input('What is the capital of',keys[key_index],'? ')

#if the answer is a value for that key
if question in keys[key_index]:

#it is correct
answer=True
#if the question choice is for governor
elif question_choice==governor:

#ask the governor question
question=input('Who is the governor of',keys[key_index],'?')

#if the answer is a value for that key
if question in keys[key_index]:

#it is correct
answer=True

return answer

主要()

当我运行程序时,出现的错误是:

state = infile.readline()File“/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/encodings/ascii.py”,第26行,在解码中
返回codecs.ascii_decode(input,self.errors)[0]
UnicodeDecodeError:'ascii'编解码器无法解码位置36的字节0xff:序数不在范围内(128)

因此,问题出在阅读文本文件的第一行。问题是什么,我该如何解决?我以前从未见过此错误。提前致谢!

最佳答案

无论您在何处打开infile,都需要使用正确的编码将其打开。具体操作方法:

  • Python 2.7:Backporting Python 3 open(encoding="utf-8") to Python 2
  • Python 3+:https://docs.python.org/3/library/functions.html#open

  • 最有可能的问题是您正在使用Python 2.7并试图打开utf-8文件。

    关于python - UnicodeDecodeError : 'ascii' codec can't decode byte 0xff in position 36: ordinal not in range(128),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47562685/

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