【问题标题】:UnicodeDecodeError: 'ascii' codec can't decode byte 0xff in position 36: ordinal not in range(128)UnicodeDecodeError:“ascii”编解码器无法解码位置 36 的字节 0xff:序数不在范围内(128)
【发布时间】:2018-05-13 17:55:01
【问题描述】:

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

该计划管理关于美国各州、首府和州长的测验

全球有多少问题

HOW_MANY_QUESTIONS=10

打开带有答案的文件

answers_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

main()

当我运行程序时,我得到的错误是:

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

所以问题源于读取文本文件的第一行;有什么问题,我该如何解决?我以前从未见过这个错误。提前致谢!

【问题讨论】:

  • 你在哪里打开infile
  • 发布完整错误!

标签: python error-handling


【解决方案1】:

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

问题很可能是您使用的是 Python 2.7 并试图打开一个 utf-8 文件。

【讨论】:

    猜你喜欢
    • 2015-07-25
    • 1970-01-01
    • 2014-12-24
    • 2011-05-13
    • 2014-02-19
    • 2018-07-26
    • 2020-11-06
    • 2014-08-12
    • 2013-09-20
    相关资源
    最近更新 更多