数学之路-python计算实战(4)-Lempel-Ziv压缩(2)
Format characters have the following meaning; the conversion between C and Python values should be obvious given their types. The ‘Standard size’ column refers to the size of the packed value in bytes when using standard size; that is, when the format string starts with one of '<', '>', '!' or '='. When using native size, the size of the packed value is platform-dependent.
本博客所有内容是原创,假设转载请注明来源
http://blog.csdn.net/myhaspl/
| Format | C Type | Python type | Standard size | Notes |
|---|---|---|---|---|
| x | pad byte | no value | ||
| c | char | string of length 1 | 1 | |
| b | signed char | integer | 1 | (3) |
| B | unsigned char | integer | 1 | (3) |
| ? | _Bool | bool | 1 | (1) |
| h | short | integer | 2 | (3) |
| H | unsigned short | integer | 2 | (3) |
| i | int | integer | 4 | (3) |
| I | unsigned int | integer | 4 | (3) |
| l | long | integer | 4 | (3) |
| L | unsigned long | integer | 4 | (3) |
| q | long long | integer | 8 | (2), (3) |
| Q | unsigned long long | integer | 8 | (2), (3) |
| f | float | float | 4 | (4) |
| d | double | float | 8 | (4) |
| s | char[] | string | ||
| p | char[] | string | ||
| P | void * | integer | (5), (3) |
Return a string containing the values v1, v2, ... packed according to the given format. The arguments must match the values required by the format exactly.
Unpack the string (presumably packed by pack(fmt, ...)) according to the given format. The result is a tuple even if it contains exactly one item. The string must contain exactly the amount of data required by the format (len(string) must equal calcsize(fmt)).
读文本文件并压缩以及解 压 ,部分代码例如以下:
# -*- coding: utf-8 -*-
#lempel-ziv算法
#code:myhaspl@myhaspl.com
import struct
mystr=""
print "\n读取源文件".decode("utf8")
mytextfile= open('test2.txt','r')
try:
mystr=mytextfile.read( )
finally:
mytextfile.close()
my_str=mystr
#码表
codeword_dictionary={}
#待压缩文本长度
str_len=len(my_str)
#码字最大长度
dict_maxlen=1
#将解析文本段的位置(下一次解析文本的起点)
now_index=0
#码表的最大索引
max_index=0 #压缩后数据
print "\n生成压缩数据中".decode("utf8")
compresseddata=[]
while (now_index<str_len):
#向后移动步长
mystep=0
#当前匹配长度
now_len=dict_maxlen
if now_len>str_len-now_index:
now_len=str_len-now_index
#查找到的码表索引。0表示没有找到
cw_addr=0
while (now_len>0):
cw_index=codeword_dictionary.get(my_str[now_index:now_index+now_len])
if cw_index!=None:
#找到码字
cw_addr=cw_index
mystep=now_len
break
now_len-=1
if cw_addr==0:
#没有找到码字,添加新的码字
max_index+=1
mystep=1
codeword_dictionary[my_str[now_index:now_index+mystep]]=max_index
print "don't find the Code word,add Code word:%s index:%d"%(my_str[now_index:now_index+mystep],max_index)
else:
#找到码字,添加新的码字
max_index+=1
if now_index+mystep+1<=str_len:
codeword_dictionary[my_str[now_index:now_index+mystep+1]]=max_index
if mystep+1>dict_maxlen:
dict_maxlen=mystep+1
print "find the Code word:%s add Code word:%s index:%d"%(my_str[now_index:now_index+now_len],my_str[now_index:now_index+mystep+1],max_index)
.......
......
my_codeword_dictionary[my_maxindex]=my_codeword_dictionary[cwkey]+cwlaster
uncompressdata.append(my_codeword_dictionary[cwkey])
uncompressdata.append(cwlaster)
print ".",
uncompress_str=uncompress_str.join(uncompressdata)
uncompressstr=uncompress_str
print "\n将解压结果写入文件里..\n".decode("utf8")
uncompress_file= open('uncompress.txt','w')
try:
uncompress_file.write(uncompressstr)
print "\n解压成功,已解压到uncompress.txt! \n".decode("utf8")
finally:
uncompress_file.close()
以下对中文维基中对python的解释文本进行压缩:
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbXloYXNwbA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
调用该程序先压缩形成压缩文件,然后打开压缩文件解压
$ pypy lempel-ziv-compress.py python.txt python.lzv
………………..
find the Code word: C add Code word: CP index:9938
index:9939de word:ython add Code word:ython
find the Code word:
^ add Code word:
^ h index:9940
find the Code word:ttp add Code word:ttp: index:9941
find the Code word:// add Code word://e index:9942
find the Code word:dit add Code word:ditr index:9943
find the Code word:a. add Code word:a.o index:9944
生成压缩数据头部
将压缩数据写入压缩文件里
…………….
. . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . .
将解压结果写入文件里..
解压成功,已解压到uncompress.txt!
查看压缩效果:
$ ls -l -h
…………….
-rw-rw-r-- 1 deep deep 5.0K Jul 1 20:55 lempel-ziv-compress.py
-rw-rw-r-- 1 deep deep 30K Jul 1 20:55 python.lzv
-rw-rw-r-- 1 deep deep 36K Jul 1 20:57 python.txt
-rw-rw-r-- 1 deep deep 36K Jul 1 20:55 uncompress.txt从上面显示结果能够看到,没压缩前为36K,压缩后为30k
压缩sqlite 3.8.5的所有源代码
$ pypy lempel-ziv-compress.py sqlitesrc.txtsqlitesrc.lzv
查看压缩效果:
$ ls -l -h
…………….
-rw-rw-r-- 1 deep deep 3.2M Jul 1 21:18 sqlitesrc.lzv
-rw-rw-r-- 1 deep deep 5.2M Jul 1 21:16 sqlitesrc.txt
-rw-rw-r-- 1 deep deep 5.2M Jul 1 21:18 uncompress.txt
没压缩前为5.2M,压缩后为3.2M
数学之路-python计算实战(4)-Lempel-Ziv压缩(2)的更多相关文章
- 数学之路-python计算实战(21)-机器视觉-拉普拉斯线性滤波
拉普拉斯线性滤波,.边缘检測 . When ksize == 1 , the Laplacian is computed by filtering the image with the follow ...
- 数学之路-python计算实战(17)-机器视觉-滤波去噪(中值滤波)
Blurs an image using the median filter. C++: void medianBlur(InputArray src, OutputArray dst, int ks ...
- 数学之路-python计算实战(5)-初识numpy以及pypy下执行numpy
N .有用的线性代数.傅里叶变换和随机数生成函数.numpy和稀疏矩阵运算包scipy配合使用更加方便.NumPy(Numeric Python)提供了很多高级的数值编程工具,如:矩阵数据类型.矢量处 ...
- 数学之路-python计算实战(20)-机器视觉-拉普拉斯算子卷积滤波
拉普拉斯算子进行二维卷积计算,线性锐化滤波 # -*- coding: utf-8 -*- #线性锐化滤波-拉普拉斯算子进行二维卷积计算 #code:myhaspl@myhaspl.com impor ...
- 数学之路-python计算实战(15)-机器视觉-滤波去噪(归一化块滤波)
# -*- coding: utf-8 -*- #code:myhaspl@myhaspl.com #归一化块滤波 import cv2 import numpy as np fn="tes ...
- 数学之路-python计算实战(14)-机器视觉-图像增强(直方图均衡化)
我们来看一个灰度图像,让表示灰度出现的次数,这样图像中灰度为 的像素的出现概率是 是图像中全部的灰度数, 是图像中全部的像素数, 实际上是图像的直方图,归一化到 . 把 作为相应于 的累计概率 ...
- 数学之路-python计算实战(2)-初遇pypy
PyPy是Python开发人员为了更好的Hack Python创建的项目.此外,PyPy比CPython是更加灵活,易于使用和试验,以制定详细的功能在不同情况的实现方法,能够非常easy实施. 该项目 ...
- 数学之路-python计算实战(19)-机器视觉-卷积滤波
filter2D Convolves an image with the kernel. C++: void filter2D(InputArray src, OutputArray dst, int ...
- 数学之路-python计算实战(9)-机器视觉-图像插值仿射
插值 Python: cv2.resize(src, dsize[, dst[, fx[, fy[, interpolation]]]]) → dst interpolation – interpol ...
随机推荐
- selenium3 + python - select定位
一.Select模块(index) 1.导入Select模块.直接根据属性或索引定位 2.先要导入select方法:from selenium.webdriver.support.se ...
- Spring Boot (6) Spring Data JPA
JPA 全称Java Persistence API,JPA通过JDK 5.0注解或xml描述对象-关系表的映射关系,并将运行期的实体对象持久化到数据库中 JPA是sun官方提出的java持久化规范, ...
- 自学Python八 爬虫大坑之网页乱码
Bug有时候破坏的你的兴致,阻挠了保持到现在的渴望.可是,自己又非常明白,它是一种激励,是注定要被你踩在脚下的垫脚石! python2.7中最头疼的可能莫过于编码问题了,尤其还是在window环境下, ...
- SQLServer2008 将“单个用户”改为“多用户”
一开始是要想要分离掉数据库,然后将其删除 不知道为什么一直分离不了,试了很多次,又尝试直接删除 结果数据库突然显示成了“单个用户” 尝试查看其属性,或者“新建查询”也都报错,提示已经有其他用户建立了连 ...
- swift-delegate(代理)或者block传值
1:delegate或者block传值 import UIKit class ViewController: UIViewController,TestDelegatePassValueDelegat ...
- 酷派改变者S1(C105/C105-6/C105-8) 解锁BootLoader 并刷入recovery root
首先下载好工具链接:https://pan.baidu.com/s/1qZjOCUw 密码:u2dr 备用下载链接:https://pan.baidu.com/s/1pMlmAef 本篇教程教你如何傻 ...
- 课上练习 script
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- SQL Server实现用户注册
用SQL Server注册用户,通过页面输入注册信息,存储到数据库. <form action="zhuChe.jsp" method="post" on ...
- MxNet教程:使用一台机器训练1400万张图片
官网链接:http://mxnet.readthedocs.io/en/latest/tutorials/imagenet_full.html Training Deep Net on 14 Mill ...
- Day7 字符串和常用数据结构
字符串和常用数据结构 使用字符串 第二次世界大战促使了现代电子计算机的诞生,当初的想法很简单,就是用计算机来计算导弹的弹道,因此在计算机刚刚诞生的那个年代,计算机处理的信息主要是数值,而世界上的第一台 ...