码迷,mamicode.com
首页 > 其他好文 > 详细

2016-01-11_11S_06day

时间:2016-01-17 21:36:51      阅读:207      评论:0      收藏:0      [点我收藏+]

标签:

一、JSON序列化

1、为什么要使用JSON:不同程序之间或不同语言之间内存数据的交互

在不同程序语言或程序之间交互数据的时候,数据的格式只有字符串格式的才可能被对端程序识别,而事实上,在数据交互的时候,大多数数据的数据类型是很复杂的,比如python的字典,而字典格式的数据是不能被别的程序语言识别的,那么久需要一个中间的媒介做翻译的功能,被该媒介翻译的数据的格式可以被其他程序识别,这种媒介就叫做JSON,类似于以前常见的XML,翻译数据的过程叫做序列化。

被JSON翻译之后任何数据都会转换为字符串类型,可以在网络(网络中只能传输字符串或二进制文件)中传输。字符串是所有程序都有的。

在将数据存入硬盘中(写入文件)的时候,数据的格式也必须是字符串格式的,JSON序列化之后的数据可以被写入文件当中。

技术分享dumps
name = {‘name‘:‘Charles‘}
import json
f  = file(‘data_to_qq‘,‘wb‘)
name_after_transfer = json.dumps(name,f)
print type(name_after_transfer)
f.write(name_after_transfer)
f.close()

E:\python\python.exe E:/python_scripts/11S_06day/json_file.py
<type ‘str‘>      #被JSON dumps之后的数据类型为字符串

技术分享
文件:data_to_qq
{"name": "Charles"}

f = file(‘data_to_qq‘,‘rb‘)
import json
name = json.loads(f.read())
f.close()
print name
print name[‘name‘]


E:\python\python.exe E:/python_scripts/11S_06day/qq_app.py
{u‘name‘: u‘Charles‘}
Charles
loads

 JSON的局限性:对于复杂的数据格式,如datetime.datetime.now(),JSON不能序列化。

2、dump/load和dumps/loads的区别:

dump直接将序列化的后的内容写入文件,而dumps是将序列化后的内容通过f.write()方法写入文件中,load/loads道理相同:

技术分享
import json
with open(‘data_to_qq‘,‘wb‘) as f:
    json.dump(name,f)

with open(‘date_to_qq‘,‘wb‘) as f:
    name_after_transfer = json.dumps(name)
    f.write(name_after_transfer)


print json.dumps(name)
print type(json.dumps(name))
dump&dumps
技术分享
import json
with open(‘data_to_qq‘,‘rb‘) as f:
    name = json.loads(f.read())
print name
print name[‘name‘]

import json
with open(‘data_to_qq‘,‘rb‘) as f:
    name = json.load(f)
print name
print name[‘name‘]
load&loads

3、pickle序列化

pickle只是针对python的,可以序列化几乎python的数据类型

二、subprocess模块

import subproces
cmd
= subprocess.check_output(["dir"],shell=True) #和.call方法类似,只是call方法在命令执行错误的时候不会报错,而check_out会报错; cmd_res = subprocess.call(["dir"],shell=True) #类似于os.system()方法

####################### subprocess.call############################

>>> res = subprocess.call("ls -l",shell=True)  #shell=True表示允许shell命令为字符串形式,如果是这样,那么前面的shell命令必须为一条命令
总用量 13296
-rw-r--r-- 1 root root 4 12月 5 06:07 456
>>> print res
0

>>> res = subprocess.call(["ls", "-l"],shell=False)      #shell=False,相当于将前面的字符串采用了.join方法
总用量 13296
-rw-r--r-- 1 root root 4 12月 5 06:07 456
>>> print res
0

#######################subprocess.check_call######################

>>> subprocess.check_call(["ls","-l"])        
总用量 13296
-rw-r--r-- 1 root root 4 12月 5 06:07 456

0
>>> subprocess.check_call("exit 1",shell=True)    #执行命令,如果执行状态码为0,则返回0,否则抛出异常
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/subprocess.py", line 511, in check_call
raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command ‘exit 1‘ returned non-zero exit status 1


>>> subprocess.check_call("exit 0",shell=True)
0

#######################subprocess.Popen############################


终端输入命令:分类

    1、输入即可输出,如ifconfig

    2、输入进入某环境,依赖该环境再输出

 

import subprocess

obj = subprocess.Popen(["python"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
obj.stdin.write(‘print 1 \n ‘)
obj.stdin.write(‘print 2 \n ‘)
obj.stdin.write(‘print 3 \n ‘)
obj.stdin.write(‘print 4 \n ‘)
obj.stdin.close()

cmd_out = obj.stdout.read()
obj.stdout.close()
cmd_error = obj.stderr.read()
obj.stderr.close()

print cmd_out
print cmd_error

 

import subprocess

obj = subprocess.Popen(["python"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
obj.stdin.write(‘print 1 \n ‘)
obj.stdin.write(‘print 2 \n ‘)
obj.stdin.write(‘print 3 \n ‘)
obj.stdin.write(‘print 4 \n ‘)

out_error_list = obj.communicate()  #communicate方法可以输出标准输出和错误输出,添加到列表中
print out_error_list

 

import subprocess

obj = subprocess.Popen(["python"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out_error_list = obj.communicate(‘print "hello"‘)   #communicate方法可以输出
print out_error_list
技术分享
>>> obj = subprocess.Popen(["python"],stdin = subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE)

>>> obj.stdin.write(‘print 1 \n‘)
>>> obj.stdin.write(‘print 2 \n‘)
>>> obj.stdin.write(‘print 3 \n‘)
>>> obj.stdin.write(‘print 4 \n‘)
>>> obj.stdin.close()

>>> cmd_out = obj.stdout.read()
>>> obj.stdout.close()
>>> cmd_error = obj.stderr.read()
>>> obj.stderr.close()

>>> print cmd_out
1
2
3
4
>>> print cmd_error
View Code
技术分享
>>> obj = subprocess.Popen(["python"],stdin = subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
>>> out_error_list = obj.communicate(‘print "hello"‘)
>>> print out_error_list
(‘hello\n‘, ‘‘)
View Code

三、Shutil模块

高级的文件、文件夹、压缩包处理模块

shutil模块对压缩包的处理是调用ZipFile 和TarFile模块进行的

 

四、日期模块

对时间的操作,有三种方式:

import time
print time.time()    #时间戳,即1970年1月1日起的秒数
print time.strftime("%Y-%m-%d")    #结构化的字符串
print time.localtime()    #结构化时间,包含年、日、星期等

E:\python\python.exe E:/python_scripts/11S_06day/time_file.py
1452996161.57
2016-01-17
time.struct_time(tm_year=2016, tm_mon=1, tm_mday=17, tm_hour=10, tm_min=2, tm_sec=41, tm_wday=6, tm_yday=17, tm_isdst=0)

  

技术分享
import time
print time.time()  #打印时间戳

import datetime
print datetime.datetime.now()

E:\python\python.exe E:/python_scripts/11S_06day/time_file.py
1452695581.91
2016-01-13 22:33:01.911000

#############################
转为之间戳:可以设定格式
print time.strftime("%Y-%m-%d %H-%S")
2016-01-13 22-51
#############################
字符串转为日期
t = time.strftime("2015-09-19","%Y-%m-%d")

############################
日期的加减(只能针对天、小时和分钟)
print datetime.datetime.now() - datetime.timedelta(days=3)#和下一条效果相同
print datetime.datetime.now() + datetime.timedelta(days=-3)
print datetime.datetime.now() - datetime.timedelta(hours=3)
print datetime.datetime.now() - datetime.timedelta(minutes=3)
View Code

五、logging日志模块

CRITICAL = 50                      #日志的等级
FATAL = CRITICAL
ERROR = 40
WARNING = 30
WARN = WARNING
INFO = 20
DEBUG = 10
NOTSET = 0

  

用于便携记录日志和线程安全的(在多线程写日志的情况下,不会造成死锁)

六、re模块

###################
match  从字符串的开头匹配
>>> import re
>>> re.match("\d","abc123def") #匹配不成功无任何输出
>>> re.match(".","abc123def")  #匹配成功返回对象
<_sre.SRE_Match object at 0x0047EA30>
>>> re.match(".","abc123def").group()  #打印输出匹配到的字符串
‘a‘

###################
search 在整个字符串中匹配
>>> re.search("\d","abc123def")  #\d表示数字
<_sre.SRE_Match object at 0x0047EA30>
>>> re.search("\d","abc123def").group()
‘1‘

>>> re.search("\d+","abc123def456ghi").group()  #+号表示重复一次或更多次
‘123‘

###################
findall #找出所有符合的字符串
>>> re.findall("\d+","abc123def456ghi_*789dd")
[‘123‘, ‘456‘, ‘789‘]
>>> re.findall("[^\d+]","abc123def456ghi_*789dd")  #找出所有非数字
[‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘, ‘f‘, ‘g‘, ‘h‘, ‘i‘, ‘_‘, ‘*‘, ‘d‘, ‘d‘]

###################
.split #分割字符串
 re.findall("[^\d+]","abc123def456ghi_*789dd")
>>> re.split("[\d+]","abc123def456ghi_*789dd")
[‘abc‘, ‘‘, ‘‘, ‘def‘, ‘‘, ‘‘, ‘ghi_*‘, ‘‘, ‘‘, ‘dd‘]
>>> re.split("\d+","abc123def456ghi_*789dd")  #以数字进行分割
[‘abc‘, ‘def‘, ‘ghi_*‘, ‘dd‘]

>>> re.split("[\d+,\*]","abc123def456ghi_*789dd")  #以数字或*分割
[‘abc‘, ‘‘, ‘‘, ‘def‘, ‘‘, ‘‘, ‘ghi_‘, ‘‘, ‘‘, ‘‘, ‘dd‘]

###################
sub 替换
>>> re.sub("ab","YUE","abc123def456ghi_*789ddabc") #将ab替换为YUE,替换所有
‘YUEc123def456ghi_*789ddYUEc‘

>>> re.sub("ab","YUE","abc123def456ghi_*789ddabc",count=1) #count参数可以设定替换几次
‘YUEc123def456ghi_*789ddabc‘
>>> re.sub("ab","YUE","abc123def456ghi_*789ddabc",count=2)
‘YUEc123def456ghi_*789ddYUEc‘
.sub包含replcce功能,replace不能替换由正则表达式匹配的字符串; ################### 匹配IP地址 >>> re.search("(\d+\.){3}(\d+)",t) <_sre.SRE_Match object at 0x004BB020> >>> re.search("(\d+\.){3}(\d+)",t).group() ‘192.168.72.1‘

精确匹配IP地址

>>> re.search("(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|
2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])",t).group()
‘192.168.72.1‘

###################
group和groups
>>> name = ‘Charles Chang‘
>>> re.search("(\w+)\s(\w+)",name)
<_sre.SRE_Match object at 0x004BB020>

>>> re.search("(\w+)\s(\w+)",name).group()
‘Charles Chang‘

>>> re.search("(\w+)\s(\w+)",name).groups()
(‘Charles‘, ‘Chang‘)

>>> re.search("(\w+)\s(\w+)",name).groups()[0]
‘Charles‘
>>>
>>> re.search("(?P<name>\w+)\s(?P<last_name>\w+)",name) #起别名
<_sre.SRE_Match object at 0x004BB020>

>>> res = re.search("(?P<name>\w+)\s(?P<last_name>\w+)",name)

>>> res.group("name")
‘Charles‘
>>> res.group("last_name")
‘Chang‘

 

#####################
转义
>>> t = "\n\tabc"
>>> print t

        abc
>>> t = r"\n\tabc"
>>> print t
\n\tabc
####################
compile   事先编译,在循环调用的使用提供效率

>>> p = re.compile("\d+")
>>> re.search(p,‘12223‘)
<_sre.SRE_Match object at 0x004505D0>
>>> re.search(p,‘12223‘).group()
‘12223‘
>>> p.match(‘123‘)
<_sre.SRE_Match object at 0x004505D0>
>>> p.match(‘123‘).group()
‘123‘
>>> p.search(‘12223‘).group()
‘12223‘
>>>
split和search的区别:
inpp = ‘1-2*((60-30 +(-40-5)*(9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 )) - (-4*3)/ (16-3*2))‘

content = re.search(‘\(([\+\-\*\/]*\d+\.*\d*){2,}\)‘,inpp).group()
before,nothing,after = re.split(‘\(([\+\-\*\/]*\d+\.*\d*){2,}\)‘,inpp,1)
print before
print nothing    
print content
print after

结果为:
E:\python\python.exe E:/python_scripts/11S_07day/re_file.py
[‘1-2*((60-30+‘, ‘-40-5‘, ‘*(9-2*5/3+7/3*99/4*2998+10*568/14))-(-4*3)/(16-3*2))‘]
1-2*((60-30+
-5
(-40-5)
*(9-2*5/3+7/3*99/4*2998+10*568/14))-(-4*3)/(16-3*2))

 

 

 

七、面向对象

################################
类初始化的两种方式:
1、
class Person(object):
    def __init__(self,name):
        self.name = name
        print "--->create:",name
    def say_name(self):
        print "My name is %s" %self.name
    def eat(self):
        print "%s is eating...." %self.name

p1 = Person("gf1")
p2 = Person("gf2")
p1.eat()

E:\python\python.exe E:/python_scripts/11S_06day/class_sample.py
--->create: gf1
--->create: gf2
gf1 is eating....
2、 class Person(object): def __init__(self,name): #self.name = name print "--->create:",name def say_name(self): print "My name is %s" %self.name def eat(self): print "%s is eating...." %self.name p1 = Person("gf1") p2 = Person("gf2") p1.name = "GF" p1.eat() E:\python\python.exe E:/python_scripts/11S_06day/class_sample.py --->create: gf1 --->create: gf2 GF is eating....

 

2016-01-11_11S_06day

标签:

原文地址:http://www.cnblogs.com/cqq-20151202/p/5121171.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!