91av视频/亚洲h视频/操亚洲美女/外国一级黄色毛片 - 国产三级三级三级三级

資源簡介

python版本為2.7.9,大家注意別下錯了,里面有一個txt文件是進行壓縮的,可以更改文件中的變量path1來對其他文件進行壓縮與解壓,代碼中有詳細注釋,實現過程雖然簡單,但是包含自己很多一些獨特的想法,自己的知識產權,所以可能貴點,謝謝大家!

資源截圖

代碼片段和文件信息

#coding:utf-8
#python?version:2.7.9
#郵箱:545989326@qq.com
#goal:用Huffman進行文件的壓縮和解壓
import?types
def?get_code(treestrcode):??????????#使用遞歸的方法從樹中得到某一個字符的Huffman編碼
????if?type(tree)==type(‘1‘):
????????return?False
????if?tree[‘0‘]==str:
????????code.append(‘0‘)
????????return?True
????elif?tree[‘1‘]==str:
????????code.append(‘1‘)
????????return?True
????else:
????????if?get_code(tree[‘0‘]strcode):
????????????code.append(‘0‘)
????????????return?True
????????if?get_code(tree[‘1‘]strcode):
????????????code.append(‘1‘)
????????????return?True
def?get_huffman_code(string):
????char_set?={}
????for?char?in?string:
????????if?char?not?in?char_set:
????????????char_set[char]=1
????????else:
????????????char_set[char]+=1
????result_d={}
????for?key?in?char_set:?????????????????#將鍵與值的位置反互換一下,有利于每次的排序
????????if?char_set[key]?not?in?result_d:
????????????result_d[char_set[key]]=key
????????else:
????????????while?char_set[key]??in?result_d:
????????????????char_set[key]+=0.01
????????????result_d[char_set[key]]=key

????while?len(result_d)!=1:?????????????#進行兩兩合并
????????char_set_tmp?=?sorted(result_d.iteritems()key=lambda?asd:asd[0]reverse=False)
????????x?=?char_set_tmp[0][0]+char_set_tmp[1][0]
????????d?=?{‘0‘:char_set_tmp[0][1]‘1‘:char_set_tmp[1][1]}
????????result_d.pop(char_set_tmp[0][0])
????????result_d.pop(char_set_tmp[1][0])
????????if?x?not?in?result_d:??????????#防止合并之后的數重復
????????????result_d[x]=d
????????else:
????????????while?x??in?result_d:
????????????????x+=0.01
????????????result_d[x]=d
????for?key?in?result_d:???
????????result_d?=??result_d[key]
????result={}
????for?key?in?char_set:
????????
????????code=[]
????????get_code(result_dkeycode)
????????str=‘‘
????????n=len(code)-1
????????while?n>=0:
????????????str+=code[n]
????????????n-=1
????????result[key]=str
????return?result
def?get_new_file(stringresultpath1):
????string_new=‘‘??????????????????????????????????????????#二進制串
????for?char?in?string:
????????string_new+=result[char]
????remain_num=7-(len(string_new)%7)???????????????????????#添加位數使得其可以整除7,然后再添加一個添加的位數的數目的一個7位的bit
????if?len(string_new)%7!=0:
????????string_new?=?string_new+(7-len(string_new)%7)*‘0‘

????f=open(path1[0:-4]+‘_new‘+‘.txt‘‘wb‘)
????i=0
????while?i????????num=?chr(int(string_new[i:i+7]2))????????????????#顯然每七位求其十進制數,然后求出對于的ascii碼,存入文件
????????f.write(str(num))
????????i+=7

????f.write(chr(remain_num))
????f.close()
def?get_past_file(stringresultpath1):???????#解壓壓縮后的文件至一個新的文件中
????result_new={}?????????????????????????????#將鍵與值的位置反互換一下,方便查找
????for?d?in?result:
????????result_new[result[d]]=d
????b_string=‘‘???????????????????????????????#還原的二進制串
????for?i?in?string:
????????s_tmp=str(bin(ord(i)))
????????s_tmp=s_tmp[2:]
????????s_tmp=‘0‘*(7-len(s_tmp))+s_tmp
????????b_string+=s_tmp
????
????k=b_string[-7:]??????????????????????????#去掉最后加入的幾個為了使其能整除7的幾位以及大小信息的7個bit
????i?=?int(k2)

?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----

?????文件???????4206??2017-11-24?09:19??huffman.py

?????文件?????190066??2017-11-22?18:20??Aesop_Fables.txt

-----------?---------??----------?-----??----

???????????????194272????????????????????2


評論

共有 條評論