Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
I';我用Python编码并不断接收:UnicodeEncodeError:';ascii';编解码器可以';t编码字符'\u2019&x27;位置31:序号不在范围内(128)_Python_Error Handling_Ascii - Fatal编程技术网

I';我用Python编码并不断接收:UnicodeEncodeError:';ascii';编解码器可以';t编码字符'\u2019&x27;位置31:序号不在范围内(128)

I';我用Python编码并不断接收:UnicodeEncodeError:';ascii';编解码器可以';t编码字符'\u2019&x27;位置31:序号不在范围内(128),python,error-handling,ascii,Python,Error Handling,Ascii,下面的代码是我在Visual Studio代码中创建的timeclienthandler.py代码。它有时可以工作,但如果我继续运行代码,它仍然会给我错误。我不明白它是怎么断断续续地工作的 from time import ctime from threading import Thread import random class TimeClientHandler(Thread): def __init__(self, client): Thread.__ini

下面的代码是我在Visual Studio代码中创建的timeclienthandler.py代码。它有时可以工作,但如果我继续运行代码,它仍然会给我错误。我不明白它是怎么断断续续地工作的

from time import ctime
from threading import Thread
import random


class TimeClientHandler(Thread):


    def __init__(self, client):
        Thread.__init__(self)
        self.client = client


    def run(self):
        msgList = ["There are some idiots who always answer 'No' to every question, now tell me. Are you one of them?","There's nothing to fear. Except maybe that weird guy over there.","If I'm driving you crazy just remember to put on your seat belt.","I wondered why the baseball was getting bigger. Then it hit me.","You're Just Jealous Because The Voices Are Talking To Me.","Quickest way to get on your feet...miss a car payment.","Why do psychics ask your name?","I'm not opinionated. I'm just always right.","Sanity is the playground for the unimaginative.","It isn't homework unless it's due tomorrow."]
        msg = msgList[random.randint(0,len(msgList))]
        msge ="\n"+msg
        self.client.send(bytes(ctime() + msge,"ascii"))
        self.client.close()

您指定的错误发生在代码的第18行

特别是-字节(ctime()+msge,“ascii”)

发生此错误的原因是您指定的“ascii”编码仅支持以下字符:

“若你们们有兴趣参加的话,他们将在今后的两个“0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 x1f'、'、'!'、''、'#'、'$'、'%'、'&'、“'、'('、')",","a,"b","c","d","e","f","g","h","i"j","k"l,"m

但是您给它的输入是'\u2019'或{'},这不在上面的列表中,因此导致UnicodeEncodeError('ascii'编解码器无法对位置0处的字符'\u2019'进行编码:序号不在范围(128))内)

因此,这个问题的解决方案是将指定的编码(ascii)更改为(UTF-8) 请尝试以下代码:

from time import ctime
from threading import Thread
import random


class TimeClientHandler(Thread):


def __init__(self, client):
    Thread.__init__(self)
    self.client = client


def run(self):
    msgList = ["There are some idiots who always answer 'No' to every question, now tell me. Are you one of them?","There's nothing to fear. Except maybe that weird guy over there.","If I'm driving you crazy just remember to put on your seat belt.","I wondered why the baseball was getting bigger. Then it hit me.","You're Just Jealous Because The Voices Are Talking To Me.","Quickest way to get on your feet...miss a car payment.","Why do psychics ask your name?","I'm not opinionated. I'm just always right.","Sanity is the playground for the unimaginative.","It isn't homework unless it's due tomorrow."]
    msg = msgList[random.randint(0,len(msgList))]
    msge ="\n"+msg
    self.client.send(bytes(ctime() + msge,"UTF-8"))
    self.client.close()

使用UTF-8编码

转到当前使用的python解释器中的文件夹
Lib\site packages
,创建名为
sitecustomize.py
的文件,并在其中添加以下代码:

#coding=utf8 
import sys 
reload(sys) 
sys.setdefaultencoding('utf8')

然后重新启动VS代码,查看问题是否消失。

您也可以尝试其他编码,如:UTF-16、UTF-32、GB18030等。如果答案有用,请向上投票。。。。。。