首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UnicodeDecodeError:'ascii‘编解码器无法解码位置36的字节0 0xff :序数不在范围内(128个)

UnicodeDecodeError:'ascii‘编解码器无法解码位置36的字节0 0xff :序数不在范围内(128个)
EN

Stack Overflow用户
提问于 2017-11-29 22:09:43
回答 1查看 1.2K关注 0票数 0

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

这个项目管理美国各州、首府和州长的测验。

全球有多少问题

HOW_MANY_QUESTIONS=10

用答案打开文件

Answers_file=open(‘quiz_responers.txt’,'r')

打开文件以在结束时添加结果

Results_file=open(‘测验_结果.quiz’,'w')

def main():

代码语言:javascript
复制
#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(婴儿):

代码语言:javascript
复制
#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(字典):

代码语言:javascript
复制
#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()

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

File"/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/encodings/ascii.py",第26行,在解码返回codecs.ascii_decode(输入,self.errors) UnicodeDecodeError:'ascii‘编解码器无法解码字节0xff位置36:序数不在范围(128)

因此,问题源于读取文本文件的第一行;问题是什么,我如何解决它?我以前从没见过这样的错误。提前感谢!

EN

回答 1

Stack Overflow用户

发布于 2017-11-29 22:47:10

无论您在何处打开infile,都需要使用正确的编码来打开它。如何做到这一点的详细信息:

最有可能的问题是,您正在使用Python2.7并试图打开utf-8文件。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47562685

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档