Python编写zlib压缩d

2024-09-28 05:22:06 发布

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

在开始之前,我使用的当前代码如下:

with open(classFileName,"a+"): as openFile
    fileData = openFile.readlines()
    try:
        fileData = zlib.decompress(str(fileData))
    except:
        pass

with open(classFileName,"a+") as openFile:
    openFile.write(fileData)

with open(classFileName,"a+") as openFile:
    fileData = []
    fileData = openFile.readlines()
    fileData.append(mergedData)

    fileData = sorted((a.strip().split() for a in fileData),key=operator.itemgetter(1))
    fileData = os.linesep.join(p[0] + ' ' + p[1] for p in fileData)

    fileData = str(fileData)
    zlib.compress(fileData)

with open(classFileName,"w") as openFile:
    openFile.write(fileData)

然而,这实际上抛出了一个错误,说在最后一行上应该有一个字符缓冲区对象。在

该文件的示例如下所示:

锐斯5号
约翰7
亚历克斯2

有什么建议或帮助吗?我想压缩以减少操纵分数。因为我这样做的原因看起来更好。不用担心原因。在


Tags: 代码inforaswith原因openwrite
1条回答
网友
1楼 · 发布于 2024-09-28 05:22:06

Python中的字符串是不能更改的(字节对象也是如此),因此,即使不查看文档,也可以推断出zlib.compress不能修改您的fileData{。上面的代码片段只记录了原始的fileData。在

来自the documentation

zlib.compress(string[, level]) Compresses the data in string, returning a string contained compressed data. level is an integer from 0 to 9 controlling the level of compression; 1 is fastest and produces the least compression, 9 is slowest and produces the most. 0 is no compression. The default value is 6. Raises the error exception if any error occurs.

所以

 fileData = zlib.compress(fileData)

应该为你工作。在

相关问题 更多 >

    热门问题