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.

本博客所有内容是原创,假设转载请注明来源

FormatC TypePython typeStandard sizeNotesxpad byteno value

ccharstring of length 11

bsigned charinteger1(3)

Bunsigned charinteger1(3)

?_Boolbool1(1)

hshortinteger2(3)

Hunsigned shortinteger2(3)

iintinteger4(3)

Iunsigned intinteger4(3)

llonginteger4(3)

Lunsigned longinteger4(3)

qlong longinteger8(2), (3)

Qunsigned long longinteger8(2), (3)

ffloatfloat4(4)

ddoublefloat8(4)

schar[]string

pchar[]string

Pvoid *integer(5), (3)

struct.pack(fmt, v1, v2, ...)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.truct.unpack(fmt, string)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:[email protected]

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

#向后移动步长

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的解释文本进行压缩:

调用该程序先压缩形成压缩文件,然后打开压缩文件解压

$ 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

lempel ziv算法c语言,数学之路-python计算实战(4)-Lempel-Ziv压缩(2)(示例代码)相关推荐

  1. 数学之路-python计算实战(14)-机器视觉-图像增强(直方图均衡化)

    我们来看一个灰度图像,让表示灰度出现的次数,这样图像中灰度为 的像素的出现概率是  是图像中全部的灰度数, 是图像中全部的像素数,  实际上是图像的直方图,归一化到 . 把  作为相应于  的累计概率 ...

  2. python中值滤波去除反光_数学之路-python计算实战(17)-机器视觉-滤波去噪(中值滤波)...

    Blurs an image using the median filter.C++:void medianBlur(InputArray src, OutputArray dst, int ksiz ...

  3. python sobel滤波,数学之路-python计算实战(22)-机器视觉-sobel非线性滤波

    sobel非线性滤波,采用梯度模的近似方式 Sobel Calculates the first, second, third, or mixed image derivatives using an ...

  4. R语言回归模型残差标准误差计算实战(Residual Standard Error):计算残差标准误、残差标准误解读

    R语言回归模型残差标准误差计算实战(Residual Standard Error):计算残差标准误.残差标准误解读 目录

  5. feedback算法C语言,Learner Reviews Feedback for 计算导论与C语言基础 Course | Coursera

    1 - 25 sur 374 Avis pour 计算导论与C语言基础 Filled StarFilled StarFilled StarStarStar par Wan K• 16 août 201 ...

  6. 行列式运算算法c语言,新手作品:行列式计算C语言版

    该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 对话     ControlHeightDecrease     Shift+Up Arrow     向上调整选定的控件或对话一个对话单位 对话     ...

  7. 行列式算法c语言,新手作品:行列式计算C语言版

    该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 对话     ControlHeightDecrease     Shift+Up Arrow     向上调整选定的控件或对话一个对话单位 对话     ...

  8. ios c语言调用oc方法,ios开发之OC基础-类和对象(示例代码)

    本系列的文章主要来自于个人在学习前锋教育-欧阳坚老师的iOS开发教程之OC语言教学视频所做的笔记,边看视频,边记录课程知识点.建议大家先过一遍视频,在看视频的过程中记录知识点关键字,把把握重点,然后再 ...

  9. c语言fmod英文全称,C语言fmod()函数:对浮点数取模(求余)(示例代码)

    头文件:#include fmod() 用来对浮点数进行取模(求余),其原型为: double fmod (double x); 设返回值为 ret,那么 x = n * y + ret,其中 n 是 ...

  10. c语言strsep,C/C++ 字符串分割: strtok 与 strsep 函数说明(示例代码)

    函数原型: char *strtok(char *s, const char *delim); char *strsep(char **s, const char *delim); 功能:strtok ...

最新文章

  1. Sping WebSocket SockJS使用
  2. springboot之@ConfigurationProperties加载配置文件
  3. python语言有什么用-为什么现在很多人都使用Python语言有什么优势
  4. 将单链表的每K个节点之间逆序
  5. 【转】在SQL Server中创建用户角色及授权(使用SQL语句)
  6. 关于使用AsyncTaskLoader的使用
  7. 安卓期末项目源码_手机随时随地写Python,还可以开发安卓APP,太厉害了!
  8. Entity Framework (EF)/Linq To entity/ ESQL(entity sql)区别 ADO.NET Entity Framework:来自微软官方的ORM框架
  9. C语言--学生管理系统--(完整代码)
  10. java 进程 cpu占用_JAVA进程CPU占用高的故障排查 – 运维那些事
  11. IOT(3)---传感器厂家
  12. 斐波那契查找算法中为什么需要把数组长度扩充到f[k]-1而不是f[k]或者f[k+1]
  13. jfinal_sql注入问题解决
  14. git小乌龟安装_ROS系统安装与体验
  15. 如何优雅的快速下载谷歌云盘的大文件 (二)
  16. 安卓熄屏录像_最屌免费安卓Android屏幕录像软件 (免ROOT)
  17. python的多元数据类型(上)
  18. python 爬取视频真实地址_python 爬取视频
  19. Ubuntu安装多用户免密登录Jupyterhub
  20. 调焦后焦实现不同距离成像_调焦与焦距的关系

热门文章

  1. laser_filters源码整体分析
  2. kettle 用cmd bat来运行ktr和kjb
  3. 【单片机毕业设计】【mcuclub-jj-037】基于单片机的电热毯的设计
  4. asp毕业设计——基于asp+access的会员管理系统设计与实现(毕业论文+程序源码)——会员管理系统
  5. 小鸡腿U T7 NEERC2011
  6. 透彻理解高斯过程Gaussian Process (GP)
  7. 历史论文比赛TCR介绍
  8. 超声波传感器(CHx01) 学习笔记 Ⅳ- 程序移植
  9. 金万维未找到服务器信息,域名解析失败原因和问题排查方法
  10. c语言汇率转换代码_基于C语言实现的货币转换器.doc