字典

>>> aa={}
>>> aa['wo']=[1,2,3,4]
>>> aa['ni']=[5,6,7,8]
>>> aa['zhang']=[20,'it','jp']
>>> aa['wu']=[30,'hr','cn']
>>> aa
{'wo': [1, 2, 3, 4], 'ni': [5, 6, 7, 8], 'wu': [30, 'hr', 'cn'], 'zhang': [20, 'it', 'jp']} 遍历字典,将字典中的一个元素的key-value对分别打出
>>> for name,info in aa.items():
... print name,info
...
wo [1, 2, 3, 4]
ni [5, 6, 7, 8]
wu [30, 'hr', 'cn']
zhang [20, 'it', 'jp'] 打出值(是一个列表)的第一个字段
>>> for name,info in aa.items():
... print name,info[0]
...
wo 1
ni 5
wu 30
zhang 20 遍历字典
>>> name
{'r': 12, 'b': 23}
>>> for k,v in name.items():
...     print k,v
...
r 12
b 23

函数

这是第一个函数
[root@kvm1 python]# python fun1.py
hello,wudealex,how are you?
[root@kvm1 python]# cat fun1.py
def sayHi(name):
print "hello,%s,how are you?" %name
n="wudealex"
sayHi(n)
这是第二个函数
[root@kvm1 python]# cat fun2.py
def sayHi(age):
if age >20:
print 'you are too old'
else:
print 'you are a young man' n=int(raw_input('please input your age!!! '))
sayHi(n) 正常情况
[root@kvm1 python]# python fun2.py
please input your age!!! 23
you are too old
[root@kvm1 python]# python fun2.py
please input your age!!! 12
you are a young man 满足了基本功能
但却有三个主要问题
其一输入字符会报错并退出
其二不输入会报错并退出
其三不能循环等待用户输入,一次就退出 [root@kvm1 python]# python fun2.py
please input your age!!! f
Traceback (most recent call last):
File "fun.py", line 7, in <module>
n=int(raw_input('please input your age!!! '))
ValueError: invalid literal for int() with base 10: 'f'
[root@kvm1 python]# python fun2.py
please input your age!!!
Traceback (most recent call last):
File "fun.py", line 7, in <module>
n=int(raw_input('please input your age!!! '))
ValueError: invalid literal for int() with base 10: ''
这是对第二个函数的改进版,解决了存留的bug

[root@kvm1 python]# cat fun3.py
def sayHi():
        while True:
                try:
                        age=int(raw_input('please input your \033[42;31m age \033[0m!!! '))
                        break
                except ValueError:
                        print 'please input a \033[46;33m number \033[0m,not string.'
        if age >20:
                print 'you are a \033[41;32m old \033[0m man '
        else:
                print 'you are a \033[43;34m young \033[0m man' sayHi()
[root@kvm1 python]# python fun3.py
please input your age!!!
please input a number,not string.
please input your age!!! e
please input a number,not string.
please input your age!!! w
please input a number,not string.
please input your age!!! -
please input a number,not string.
please input your age!!! 12
you are a young man
[root@kvm1 python]# python fun1.py
please input your age!!! 34
you are too old
函数的默认参数,
即c=0
或空c='' import os def sshcmd(a,b,c):
sh= 'ssh %s@%s %s' %(a,b,c)
# print sh
os.system(sh) user='root'
host='192.168.10.103'
cmd='hostname' sshcmd(user,host,cmd)

pickle模块

pickle序列化,不像文件,plk文件不需要转来转去,如果用文件来中转的话,需要遍历字典,还要输出到文件中,读的时候也较麻烦
所以pickle模块提供了一种中间物,方便共享数据。原样存,原样取。便于移植 定义
>>> import tab
>>> import pickle
>>> acc={}
>>> acc['']=['a',15,14]
>>> acc['']=['b',30,40]
>>> acc
{'': ['a', 15, 14], '': ['b', 30, 40]}
>>> f =file('acc.pkl','wb')
>>> pickle.dump(acc,f)
>>> f.close() 假如pickle.dump后,又需要修改某个值,
acc[''][0]='c'
这个时候如果再一次的
pickle.dump(acc,f),那么acc.pkl文件会有两组字典,所以会对load时产生混乱,
所以最好dump一次就close一次,再修改,再打开 取用
>>> pkl_file=open('acc.pkl','rb')
>>> acc_info=pickle.load(pkl_file)
>>> acc_info
{'': ['a', 15, 14], '': ['b', 30, 40]}

re模块

>>> m=re.match(r'ni','ni hao')
>>> print m.group()
ni
>>> m=re.match(r'ii','ni hao')
>>> print m.group()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'group' >>> p=re.compile(r'\d+')
>>> print p.split('one1two2three3four4')
['one', 'two', 'three', 'four', '']
>>> p=re.compile(r'\D+')
>>> print p.split('one1two2three3four4')
['', '', '', '', '']
04-22 21:52