[toc]
列表的封装与解压
In [10]: lst = [1,[2,3,6,7,8,10],4]
In [11]: a, (b, *_, c),d = lst
In [12]: a
Out[12]: 1
In [13]: b
Out[13]: 2
In [14]: c
Out[14]: 10
In [15]: d
Out[15]: 4
输出格式练习
In [18]: 'i am %s' %('renjin')
Out[18]: 'i am renjin'
In [24]: 'i am %(name)s' %{'name':'renjin'}
Out[24]: 'i am renjin'
In [29]: 'i am %s my name is %s' %('ssjinyao','renjin')
Out[29]: 'i am ssjinyao my name is renjin'
format 输出格式
In [3]: 'I am {}'.format('ssjinyao')
Out[3]: 'I am ssjinyao'
In [4]: 'I am {} my age is {}'.format('ssjinyao',23)
Out[4]: 'I am ssjinyao my age is 23'
In [5]: 'I am {1} my age is {0}'.format(23,'ssjinyao')
Out[5]: 'I am ssjinyao my age is 23'
In [6]: 'I am {name} my age is {age}'.format(age='23',name='ssjinyao')
Out[6]: 'I am ssjinyao my age is 23'
In [8]: 'I am {0} my name is {0}'.format('ssjinyao')
Out[8]: 'I am ssjinyao my name is ssjinyao'
In [15]: '{} name {} and age is {age}'.format('renjin','ssjinyao',age='23')
Out[15]: 'renjin name ssjinyao and age is 23'
In [16]: class A():
...: def __init__(self):
...: self.x=1
...: self.y=2
...:
In [17]: a = A()
In [18]: a.x
Out[18]: 1
In [19]: a.y
Out[19]: 2
In [24]: '{inst.x}'.format(inst=a)
Out[24]: '1'
In [20]: '{0.x} {0.y}'.format(a)
Out[20]: '1 2'
In [26]: '{0!a}'.format('逝水锦遥')
Out[26]: "'\\u901d\\u6c34\\u9526\\u9065'"
In [27]: '{0:^80}'.format('逝水锦遥')
Out[27]: ' 逝水锦遥 '
In [29]: '{0:<80}'.format('逝水锦遥')
Out[29]: '逝水锦遥 '
In [30]: '{0:>80}'.format('逝水锦遥')
Out[30]: ' 逝水锦遥'
In [34]: '{0:{fill}^{width}}'.format('逝水锦遥',width='80',fill='*')
Out[34]: '**************************************逝水锦遥**************************************'
In [35]: '{message:{fill}^{width}}'.format(message='逝水锦遥',width='80',fill='*')
Out[35]: '**************************************逝水锦遥**************************************'
In [41]: s = '逝水锦遥'
In [42]: b = s.encode()
In [43]: for x in s:
...: print (x)
...:
逝
水
锦
遥
In [44]: for x in b:
...: print (x)
...:
233
128
157
230
176
180
233
148
166
233
129
165
In [44]: for x in b:
...: print (x)
...:
233
128
157
230
176
180
233
148
166
233
129
165
In [45]: b.decode()
Out[45]: '逝水锦遥'
python3 格式转换
(py3_env) [root@laoba-10-17:~/mypython/python3]# cat <<EOF >> utf-8.txt
逝水锦遥
EOF
(py3_env) [root@laoba-10-17:~/mypython/python3]# iconv -f UTF-8 -f UTF-8 -t GBK utf-8.txt
��ˮ��ң
(py3_env) [root@laoba-10-17:~/mypython/python3]# iconv -f UTF-8 -f UTF-8 -t GBK utf-8.txt > gbk.txt
(py3_env) [root@laoba-10-17:~/mypython/python3]# cat gbk.txt
��ˮ��ң
In [6]: f = open('/root/mypython/python3/gbk.txt','rb')
In [7]: buf = f.read()
In [8]: buf.decode('GBK')
Out[8]: '逝水锦遥\n'
In [9]: name = buf.decode('GBK')
In [10]: print (name)
逝水锦遥
基于socket 之间的编码格式转换
# 服务端
In [14]: import socket
In [15]: s = socket.socket(family=socket.AF_INET, type=socket.SOCK_STREAM)
In [16]: s.bind(('0.0.0.0',3000))
In [17]: s.listen()
In [18]: fd, addr = s.accept()
# 客户端
In [1]: import socket
In [2]: s = socket.socket(family=socket.AF_INET, type=socket.SOCK_STREAM)
In [4]: so = s.connect(('127.0.0.1',3000))
In [5]: s.send('逝水锦遥'.encode())
Out[5]: 12
# 服务端
In [33]: fd.recv(1024)
Out[33]: b'\xe9\x80\x9d\xe6\xb0\xb4\xe9\x94\xa6\xe9\x81\xa5'
# 客户端
In [22]: s.send('逝水锦遥'.encode())
Out[22]: 12
# 服务端
In [40]: buf = fd.recv(1024)
In [41]: buf.decode()
Out[41]: '逝水锦遥'
# 客户端
In [23]: s.send('乐乐在线'.encode())
Out[23]: 12
# 服务端
In [42]: buf = fd.recv(1024)
In [43]: buf.decode()
Out[43]: '乐乐在线'
# 客户端
In [24]: s.send('逝水锦遥,乐乐在线'.encode())
Out[24]: 27
# 服务端
In [45]: buf = fd.recv(1024)
In [46]: buf.decode()
Out[46]: '逝水锦遥,乐乐在线'
基于 socket 之间的json格式传输
# 客户端生成json格式的数据并发送
In [26]: import json
In [27]: data = {'name':'ssjinyao',}
In [28]: data = {'name':'ssjinyao','owner':'renjin'}
In [30]: json.dumps(data)
Out[30]: '{"name": "ssjinyao", "owner": "renjin"}'
In [31]: js = json.dumps(data)
In [32]: s.send(js.encode())
Out[32]: 39
# 服务端接收发送过来的json数据
In [47]: buf = fd.recv(1024)
In [48]: buf.decode()
Out[48]: '{"name": "ssjinyao", "owner": "renjin"}'
# 一般发送端会指定编码
In [34]: s.send(js.encode('UTF-8'))
Out[34]: 39
# 接收端也会指定编码,这样不会出现乱码、
In [50]: buf.decode('UTF-8')
Out[50]: '{"name": "ssjinyao", "owner": "renjin"}'