UnicodeEncodeError:“latin1”编解码器无法对1112位置的字符进行编码:序号不在范围内(256)

2024-04-26 21:25:27 发布

您现在位置:Python中文网/ 问答频道 /正文

下面的代码从游戏服务器下载一个日志,并将其上传回网站进行显示。但是,当bot本身不读取日志时,我会收到一个UnicodeError。我正在使用“logalyzer.exe”解析这些日志,一旦它们到达我的机器。当我在不使用bot的情况下使用logalyzer.exe解析日志时,它可以很好地解析日志,但在使用此bot时。。它给出了标题中的unicode错误

仅当玩家在其姓名中使用特殊字符时发生

编辑:根据MSVC代码。。第22行是罪魁祸首。。ftp.storbinary('STOR%s'%f,fh)。。会调查原因,除非有人知道

def uploadThis(path, ftp):
    files = os.listdir(path)
    os.chdir(path)
    for f in files:
        if os.path.isfile(path + r'\{}'.format(f)):
            fh = open(f, 'rb')
            ftp.storbinary('STOR %s' % f, fh)
            fh.close()
        elif os.path.isdir(path + r'\{}'.format(f)):
            ftp.mkd(f)
            ftp.cwd(f)
            uploadThis(path + r'\{}'.format(f), ftp)
    ftp.cwd('..')
    os.chdir('..')

    
@client.command(pass_context=True)
@commands.has_role('Sanitation Engineers')
@commands.cooldown(1, 300, commands.BucketType.guild)
async def stats(ctx):
    await ctx.send("Creating and Foomalyzing server logs.. this may take at least 1 minute...")    
    logToParse1 = ""
    logToParse2 = ""
    roundOneLink = ""
    roundTwoLink = ""
    path = variables['path']
    imagePath = variables['imagepath']  
    stylePath = variables['stylepath']

    ftp = FTP(variables['FTPHOST'], user = variables['USER'], passwd= variables['PASSWORD'])
    ftp.cwd(variables['ftppath'])
    dirList = ftp.nlst()
    logs = []
    c = -1
    print(dirList)
    for i in dirList:
        if(".log" in i):
            logs.append(i)
    for i in range(len(logs)):
        if(int(ftp.size(logs[c])) > 60000):
            logToParse2 = logs[c]
            print(logToParse2)
            break
        else:
            c = c - 1
    c = c - 1
    for i in range(len(logs)):
        if(int(ftp.size(logs[c])) > 60000):
            logToParse1 = logs[c]
            print(logToParse1)
            break
        else:
            c = c - 1
    parseFolder1 = logToParse1[:-4]
    parseFolder2 = logToParse2[:-4]
    ftp.retrbinary("RETR " + logToParse1, open(logToParse1, 'wb').write)
    ftp.retrbinary("RETR " + logToParse2, open(logToParse2, 'wb').write)
    ftp.quit()

    ftp = FTP(variables['SITEHOST'], user = variables['USER2'], passwd= variables['PASSWORD2'])
    ftp.cwd('public')
    ftp.cwd('logs')
    print(ftp.nlst())
    siteList = ftp.nlst()

    if (parseFolder1 in siteList):
        print("Folder already exists, no upload necessary")
        roundOneLink = variables["URLPATH"] + parseFolder1 + "/"
    else:
        roundOneLink = variables["URLPATH"] + parseFolder1 + "/"
        os.mkdir(parseFolder1)
        desPath = variables['despath'] + parseFolder1

        os.system('cmd /c ' + variables['DRIVE'] + ' && cd %s && logalyzer -image %s -style %s -o %s %s' % (path, imagePath, stylePath, parseFolder1, logToParse1))
        ftp.mkd(parseFolder1)
        ftp.cwd(parseFolder1)
        uploadThis(desPath, ftp)
        os.remove(logToParse1)
        shutil.rmtree(path + parseFolder1)

    if (parseFolder2 in siteList):
        print("Folder already exists, no upload necessary")
        roundTwoLink = variables["URLPATH"] + parseFolder2 + "/"
    else:
        roundTwoLink = variables["URLPATH"] + parseFolder2 + "/"
        os.mkdir(parseFolder2)
        desPath = variables['despath'] + parseFolder2

        os.system('cmd /c ' + variables['DRIVE'] + ' && cd %s && logalyzer -image %s -style %s -o %s %s' % (path, imagePath, stylePath, parseFolder2, logToParse2))
        ftp.mkd(parseFolder2)
        ftp.cwd(parseFolder2)
        uploadThis(desPath, ftp)
        os.remove(logToParse2)
        shutil.rmtree(path + parseFolder2)

    await ctx.send("Round 1: " + roundOneLink)
    await ctx.send("Round 2: " + roundTwoLink)

    ftp.quit()

Tags: pathinifosftpvariableslogsprint