我正在做一个python练习,要求我编写一个Lempel-Ziv算法来压缩文本文件。当我写压缩文件时,输出文件高于第一个。那是我的代码:
with open ("quijote.txt", "r") as myfile:
data=myfile.read()
datab = ''.join(format(ord(x), 'b') for x in data)
BitPos = {'':0}
PosAdded = {}
i = 0
for f in range(len(datab)):
if datab[i:f] not in BitPos:
BitPos[datab[i:f]] = format(i+1,'b')
PosAdded[BitPos[datab[i:f-1]]] = datab[f-1:f]
i = f
maxl = len(str(format(i,'b')))
compres = ''
for pos in PosAdded:
compres += str(pos).zfill(maxl)+PosAdded[pos]
compres = format(maxl+1,'b')+'\n'+compres
f = open('quijote3.txt','w')
f.write(compres)
f.close()
输入文件为2MB,输出为9MB。