您的位置:首页 > 数据库 > Oracle

Oracle Import and Export

2014-06-03 20:04 429 查看
转自:http://muxu303.blog.163.com/blog/static/51280192011224101232405/

python作为脚本语言中最给力的一种,集成了编译语言的功能性和脚本语言的灵活性,作为一种高级的解释性脚本语言,同时还具有程序语言的各种优点如:面向对象,可扩展,可移植,易学易用,强大的内存管理,有点少叙,本文介绍其对二进制数据的操作模块struct,无论是网络应用,还是文件存储,均可派上用场..
先来感性的认识一段代码:如下(输出为斜体)
 
--------------------------------------------------------------------------------
>>> import struct  【引入struct包】
>>> byte_buf = struct.pack("i5sc6s", 9527,"hello"," ","world!!")  【打包数据到byte_buf中,参数“i5sc6s”表示一个int型,长度为5的char组...】
>>> v1,v2,v3,v4 = struct.unpack("i5sc6s",byte_buf) 【解包到四个值】
>>> print v1,v2,v3,v4 【打印输出】
9527 hello   world!                                                              【最后一个感叹号没打印出来,因为格式化参数“i5sc6s“的6截断第二个感叹号】
--------------------------------------------------------------------------------
 
 
>>> byte_buf = struct.pack("i", 134)    【只对一个值打包】
>>> type(struct.unpack("i", byte_buf))                                    【unpack返回的是tuple
 
<type 'tuple'>
>>> struct.unpack("i", byte_buf)[0]     
134
通过如上的演示,相信大家对该模块的用法已经掌握了,如下两表为该模块对应的,格式化时使用的数据类型,和一些特殊规则
 
 

Format
C Type
Python
字节数
xpad byteno value1
ccharstring of length 11
bsigned charinteger1
Bunsigned charinteger1
?_Boolbool1
hshortinteger2
Hunsigned shortinteger2
iintinteger4
Iunsigned intinteger or long4
llonginteger4
Lunsigned longlong4
qlong longlong8
Qunsigned long longlong8
ffloatfloat4
ddoublefloat8
schar[]string1
pchar[]string1
Pvoid *long4
 
考虑到c或c++编译器使用了字节对齐,通常是以4个字节为单位的32位系统,故而还提供了如下的选项用来表示不同的字节对其方式,如使用'@6si'表示4字节对齐,先6个char,再一个int型,共使用12个字节
 
Character
Byte order
Size and alignment
@nativenative            4个字节对齐
=nativestandard        原字节数
<little-endianstandard        原字节数
>big-endianstandard       原字节数
!network (= big-endian)standard       原字节数
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: