易截截图软件、单文件、免安装、纯绿色、仅160KB
热门标签: c c# c++ asp asp.net linux php jsp java vb Python Ruby mysql sql access Sqlite sqlserver delphi javascript Oracle ajax wap mssql html css flash flex dreamweaver xml
 最新文章 : Python

Python模块学习

  有时候,要把内存中的一个对象持久化保存到磁盘上,或者序列化成二进制流通过网络发送到远程主机上。Python中有很多模块提供了序列化与反序列化的功能,如:marshal, pickle, cPickle等等。今天就讲讲marshal模块。
  注意:
marshal并不是一个通用的模块,在某些时候它是一个不被推荐使用的模块,因为使用marshal序列化的二进制数据格式还没有文档化,在不同版本的Python中,marshal的实现可能不一样。也就是说,用python2.5序列为一个对象,用python2.6的程序反序列化所得到的对象,可能与原来的对象是不一样的。但这个模块存在的意义,正如Python手册中所说:The marshal
module exists mainly to support reading and writing the “pseudo-compiled” code for Python modules of .pyc
files.
下面是marshal模块中定义的一些与序列化/反序列化有关的函数:
marshal.dump(value, file[, version])
  将值写入到一个打开的输出流里。参数value表示待序列化的值。file表示打开的输出流。如:以”wb”模式打开的文件,sys.stdout或者os.popen。对于一些不支持序列类的类型,dump方法将抛出ValueError异常。要特别说明一下,并不是所有类 ......

Python入门的36个例子 之 18

例1:
# _018
# This is a module (if write Chinese in a module, there will be a error)
def func1():
print 'This is function 1.'
def func2():
print 'This is function 2.'
def func3():
print 'This is function 3.'
# 019
# 使用“import”语句调用模块:
import _018_Module
_018_Module.func1()
_018_Module.func2()
_018_Module.func3()

output:
This is function 1.
This is function 2.
This is function 3.
例2:
# _018
# This is a module (if write Chinese in a module, there will be a error)
def func1():
print 'This is function 1.'
def func2():
print 'This is function 2.'
def func3():
print 'This is function 3.'

# 020
# 使用“from ... import ...”语句调用模块
from _018_Module import func2
import _018_Module
_018_Module.func1()
func2()
_018_Module.func3()

output:
This is function 1.
This is function 2.
This is function 3.
例3:
# _018
# This is a module (if write Chinese in a module, there will be a error)
de ......

Python入门的36个例子 之 19

源代码下载:下载地址在这里
# 022
listNum1 = [1, 3]
listNum2 = [2, 4]
listStr1 = ['a', 'c']
listStr2 = ['b', 'd']
# 列表的合并
list1 = listNum1 + listStr1
for ele in list1:
print ele
print '\n'
# 判断列表中是否包含某元素
print 'b' in list1
print 1 in list1
# 删除某个元素
for ele in list1:
print ele
del list1[1]
for ele in list1:
print ele
print '\n'
# 对列表进行排序
list1 = listNum1 + listNum2 + listStr1 + listStr2
for ele in list1:
print ele
print '\n'
list1.sort()
for ele in list1:
print ele
print '\n'
# 对列表进行倒排序
list1 = listNum1 + listNum2 + listStr1 + listStr2
for ele in list1:
print ele
print '\n'
list1.sort(reverse = True)
for ele in list1:
print ele
print '\n'
# 对列表进行逆序
list1 = listNum1 + listNum2 + listStr1 + listStr2
for ele in list1:
print ele
print '\n'
list1.reverse()
for ele in list1:
print ele
print '\n'
# 删除列表中连续的一部分
list2 = listNum1 + listNum2 + listStr1 + listS ......

Python入门的36个例子 之 19

源代码下载:下载地址在这里
# 023
# Tuple(元素组)是不可变的列表
tuple1 = (1, 2, 3)
print tuple1
tuple2 = (tuple1, 4, 5, 6) # 一个元素组可以作为另外一个元素组的元素
print tuple2 # 并且能够在存储的时候保持原始的逻辑关系
for ele in tuple2:
print ele
print '\n'
for ele in tuple2[0]:
print ele
print '\n'
# 元素组与格式化输出
age = 22
name = 'Ning'
print '%s is %d years old.' % (name, age)
print 'Hello, %s' % name # 一个元素的时候可以不用元素组

(1, 2, 3)
((1, 2, 3), 4, 5, 6)
(1, 2, 3)
4
5
6
1
2
3
Ning is 22 years old.
Hello, Ning ......

Python入门的36个例子 之 21

源代码下载:下载地址在这里
# 024
dict1 = {
'5064001':'Mememe',
'5064002':'tutu',
'5064003':'thrthr',
'5064004':'fofo'
}
print dict1['5064003']
# 也可以使用整型作为唯一的编号
dict2 = {
5064001:'Mememe',
5064002:'tutu',
5064003:'thrthr',
5064004:'fofo'
}
print dict2[5064003]
# 添加
dict2[5064000] = 'none'
print dict2[5064000]
del dict2[5064002]
print dict2
for ele in dict2:
print ele
for id, name in dict2.items():
print id, name

output:
thrthr
thrthr
none
{5064000: 'none', 5064001: 'Mememe', 5064003: 'thrthr', 5064004: 'fofo'}
5064000
5064001
5064003
5064004
5064000 none
5064001 Mememe
5064003 thrthr
5064004 fofo ......

Python入门的36个例子 之 22

源代码下载:下载地址在这里
# 025
# 序列的神奇之处在于你可以使用相同的方式tuple、list和string
se = ['a', 'b', 'c', 'd']
li = ['a', 'b', 'c', 'd']
tu = ('a', 'b', 'c', 'd')
string = 'abcd'
print se[1]
print li[1]
print tu[1]
print string[1]
# 序列的另外一个神奇之处在于,你可以使用负数进行索引
print se[1:-1] # -1表示倒数第一个
print se[:] # 这种表达形式相当于访问整个元素集合

output:
b
b
b
b
['b', 'c']
['a', 'b', 'c', 'd'] ......
总记录数:695; 总页数:116; 每页6 条; 首页 上一页 [103] [104] [105] [106] 107 [108] [109] [110] [111] [112]  下一页 尾页
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号