UnicodeDecodeError: 'utf8' codec can't decode byte 0x9c

前端 未结 10 2029
暖寄归人
暖寄归人 2020-11-22 14:49

I have a socket server that is supposed to receive UTF-8 valid characters from clients.

The problem is some clients (mainly hackers) are sending all the wrong kind of

10条回答
  •  长发绾君心
    2020-11-22 15:29

    the first,Using get_encoding_type to get the files type of encode:

    import os    
    from chardet import detect
    
    # get file encoding type
    def get_encoding_type(file):
        with open(file, 'rb') as f:
            rawdata = f.read()
        return detect(rawdata)['encoding']
    

    the second, opening the files with the type:

    open(current_file, 'r', encoding = get_encoding_type, errors='ignore')
    

提交回复
热议问题