zoukankan      html  css  js  c++  java
  • python struct字节操作pack及copy及基础

          1、在python环境下,要想运行PY文件,必须用导入模块的方式实现,即

    import exam

          可是每次只能导入一次,导入第二次时就不会再运行文件,如果实在想运行的话可以用函数:

    reload(exam)   在你不停止python或重启的情况下,导入修改后的模块时使用。

    括号里的参数必须是已经加载成功了的模块名称,输出多的那一行,即最后一行是函数的返回值的显示,是模块对象。import 是语句。

          2、== 与is ,两者都是返回的布尔类型,前者是判断两边是否相等,后者是判断两个变量是否引用的同一个对象。当对象为小数时,即如a=12、b=a=12,两者返回结果一样,这只是个例外。

    a is b=True,

          3、sys模块里有一个函数getrefcount()可以查看对象的引用次数。

    import sys

    sys.getrefcount(object)

          4、python里的copy()函数

     >>> import copy
    >>> listOne = [{"name": "Willie", "city": "Providence, RI"}, 1, "tomato", 3.0]

    >>> listTwo = listOne[:] # or listTwo=copy.copy(listOne)
    >>> listThree = copy.deepcopy(listOne)
    >>> listOne.append("kid")
    >>> listOne[0]["city"] = "San Francisco, CA"
    >>> print listOne, listTwo, listThree
    [{'name': 'Willie', 'city': 'San Francisco, CA'}, 1, 'tomato', 3.0, 'kid']
    [{'name': 'Willie', 'city':'San Francisco, CA'}, 1, 'tomato', 3.0]
    [{'name': 'Willie', 'city': 'Providence, RI'}, 1, 'tomato', 3.0]

     由以上运行结果得出,浅度复制只对列表里的第一层成员有作用,却没法影响到字典。深度复制可以深入到字典内部,产生完全两个不一样的对象。这就是两者区别。

           5、临时文件处理

           Python 提供了一个不错的小模块tempfile,
    它发布了两个函数:mktemp()和TemporaryFile()。前者返回你机器的临时文件目录(如Unix 里的/tmp 和Windows 的 c:\tmp)中未使用的文件名, 后者直接返回一个文件对象。例如:
    # 读输入文件
    inputFile = open('nput.txt', 'r')
    import tempfile
    # 创建临时文件
    tempFile = tempfile.TemporaryFile() # 我们甚至不需要知道文件名
    first_process(input = inputFile, output = tempFile)

    # 创建输出文件
    outputFile = open('output.txt', 'w')
    second_process(input = tempFile, output = outputFile)

    (未完待续。。)

           6、 exec   语句用来执行储存在字符串或文件中的Python语句。例如,我们可以在运行时生成一个包
    含Python代码的字符串,然后使用exec语句执行这些语句。下面是一个简单的例子。
    >>> exec 'print "Hello World"'
    Hello World
    eval语句用来计算存储在字符串中的有效Python表达式。下面是一个简单的例子。
    >>> eval('2*3')

           7、struct.pack() and struct.unpack()

    用于C语言数据与Python数据类型间转换。

    Character Byte order Size Alignment
    @ native native native
    = native standard none
    < little-endian 小尾字节序 standard none
    > big-endian standard none
    ! network (= big-endian) standard none

    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)

    >>> from struct import *
    >>> pack('hhl', 1, 2, 3)    #本例是大尾字节序
    '\x00\x01\x00\x02\x00\x00\x00\x03'
    >>> unpack('hhl', '\x00\x01\x00\x02\x00\x00\x00\x03')
    (1, 2, 3)
    >>> calcsize('hhl')    #参数必须是类型fmt
    8
    >>> pack('ci', '*', 0x12131415)   # 存数时自动对齐字节,传说中的字节对齐,如果加上等号则进行优化,也就是说不填充字节
    '*\x00\x00\x00\x12\x13\x14\x15'
    >>> pack('ic', 0x12131415, '*')
    '\x12\x13\x14\x15*'
    >>> calcsize('ci')   如calcsize('=ci')  结果是:5
    8
    >>> calcsize('ic') 
    5
    
    >>>pack('llh0l', 1, 2, 3)        #加0在后面填充两个字节的0,不是很清楚,留待以后操作验明。
    '\x00\x00\x00\x01\x00\x00\x00\x02\x00\x03\x00\x00'
     #抄袭一把,(:
    1. # 取前5个字符,跳过4个字符华,再取3个字符   
    2. format = '5s 4x 3s'  
    3. 2. 使用struck.unpack获取子字符串   
    4. import struct   
    5. print struct.unpack(format, 'Test astring')   
    6. #('Test', 'ing')   
    7. 来个简单的例子吧,有一个字符串'He is not very happy',处理一下,把中间的not去掉,然后再输出。   
    8. import struct   
    9. theString = 'He is not very happy'   
    10. format = '2s 1x 2s 5x 4s 1x 5s'   
    11. print ' '.join(struct.unpack(format, theString))   
    12. 输出结果:   
    13. He is very happy  
  • 相关阅读:
    Murano为镜像包添加Root用户密码
    Murano Weekly Meeting 2015.09.22
    Murano Weekly Meeting 2015.09.15
    Murano Weekly Meeting 2015.09.08
    Murano Deployment
    Murano Weekly Meeting 2015.09.01
    Murano Application
    Mongodb installation & userguide
    查看linux系统常用的命令,Linux查看系统配置常用命令
    SQL必知必会笔记
  • 原文地址:https://www.cnblogs.com/lovemo1314/p/1869196.html
Copyright © 2011-2022 走看看