第19次全天课笔记 20181202 json
第19次全天课笔记 20181202 WiFi密码:46702126
正则表达式练习
1 统计文章的字母个数
>>> re.findall(r"[a-zA-Z]","ab 78 cd 69\n")
['a', 'b', 'c', 'd']
>>> len(re.findall(r"[a-zA-Z]","ab 78 cd 69\n"))
4
2统计文章数字个数
>>> len(re.findall(r"\d","ab 78 cd 69\n"))
4
3统计文章中的单词个数
>>> len(re.findall(r"\b[a-zA-Z]+\b","ab 78 cd 69\n"))
2
4统计文章单词个数
5获取某个网页的所有链接
re.findall(r'href="(.*?)"',s)
6 删除文章中所有数字
>>> re.sub(r"\d+","","a123 a56789")
'a a'
7 删除后统计一下删除个数
>>> re.subn(r"\d+","","a123 b456 c789")
('a b c', 3)
>>> re.subn(r"\d+","","a123 b456 c789")[1]
3
8 匹配一个ip
re.match(r"([2][0-4][0-9]\.|[2][5][0-5]\.|[1][0-9]{2}\.|[1-9][0-9]{0,1}\.|0\.)([2][0-4][0-9]\.|[2][5][0-5]\.|[1][0-9]{2}\.|[1-9][0-9]{0,1}\.|0\.)([2][0-4][0-9]\.|[2][5][0-5]\.|[1][0-9]{2}\.|[1-9][0-9]{0,1}\.|0\.)([2][0-4][0-9]|[2][5][0-5]|[1][0-9]{2}|[1-9][0-9]{0,1}(?!\d)|0)"," 255.20.255.255")
或((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.){3}(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]) #还要加上259 不匹配25
9 统计开头不是a的所有单词
>>> re.findall(r"\b(?!a)[a-zA-Z]+\b","abc cde fgi 2ad ")
['cde', 'fgi']
10 匹配1900年到2999年
>>> re.match(r"(19\d{2}|2\d{3})年","1920年")
<_sre.SRE_Match object; span=(0, 5), match='1920年'>
Json串
#coding=utf-8
import json
data= [ { 'a':'A', 'b':(2, 4), 'c':3.0, (1,2):'D tuple' } ]
print(u"不设置skipkeys 参数")
try :
res1 = json.dumps(data) #skipkeys参数默认为False时
except Exception as e:
print(e)
print(u"设置skipkeys 参数")
print(json.dumps(data, skipkeys=True))# skipkeys=True时
>>> import json
>>> print (json.dumps('中国’))
"\u4e2d\u56fd"
>>> print (json.dumps('中国',ensure_ascii=False))
"中国"
>>> #coding=utf-8
... import json
>>> data = [{'a':"Aasdf",'b':(2,4),'c':3.0}]
>>> data_json = json.dumps(data)
>>> print("encoding :", data_json)
encoding : [{"a": "Aasdf", "b": [2, 4], "c": 3.0}]
>>> print("decoding :", json.loads(data_json))
decoding : [{'a': 'Aasdf', 'b': [2, 4], 'c': 3.0}]
#encoding=utf-8
import json
class Employee(object):
def __init__(self, name, age, sex, tel):
self.name = name
self.age = age
self.sex = sex
self.tel = tel
# 将序列化函数定义到类里面
def obj_json(self, obj_instance):
return {
'name': obj_
instance.name,
'age': obj_instance.age,
'sex': obj_instance.sex,
'tel': obj_
instance.tel }
emp = Employee('Lily', 24, 'female', '18223423423')
print(json.dumps(emp, default = emp.obj_json))
#encoding=utf-8
import json
class Employee(object):
def __init__(self, name, age, sex, tel):
self.name = name
self.age = age
self.sex = sex
self.tel = tel
emp = Employee('Lily', 24, 'female', '18223423423')
print(emp.__dict__)
print(json.dumps(emp, default = lambda Employee: Employee.__dict__))
print(json.dumps(emp, default = lambda emp: emp.__dict__))
#encoding=utf-8
import json
class Employee(object):
def __init__(self, name, age, sex, tel):
self.name = name
self.age = age
self.sex = sex
self.tel = tel
emp = Employee('Lily', 24, 'female', '18223423423')
def jsonToClass(emp):
return Employee(emp['name'], emp['age'], emp['sex'], emp['tel'])
json_str = '{"name": "Lucy", "age": 21, "sex": "female", "tel": "15834560985"}'
e = json.loads(json_str, object_hook = jsonToClass)
print(e)
print(
e.name)
深入浅出MySql,红白相间
XML
判断节点是文本还是element
D:\up\1202
>>> isinstance(books[0].childNodes[0],xml.dom.minidom.Text)
True
>>> isinstance(books[0].childNodes[1],xml.dom.minidom.Element)
True
#从xml.dom.minidom模块引入解析器parse
from xml.dom.minidom import parse
DOMTree = parse("movies.xml")
movielist = DOMTree.documentElement
movies = movielist.getElementsByTagName("movie")
#遍历movies节点,找到movie节点下的子节点,然后打印值
for movie in movies:
if movie.hasAttribute("title") and movie.getAttribute("title")=="Trigun":
print (movie.getAttribute("title"))
for i in movie.childNodes[1::2]:
print (i.tagName,end=":")
print (i.childNodes[0].data)
#coding=utf-8
import xml.dom.minidom
#在内存中创建一个空的文档
doc = xml.dom.minidom.Document()
#创建一个根节点company对象
root = doc.createElement('companys')
# 给根节点root添加属性
root.setAttribute('name', u'公司信息')
#将根节点添加到文档对象中
doc.appendChild(root)
# 给根节点添加一个叶子节点
company = doc.createElement('gloryroad')
# 叶子节点下再嵌套叶子节点
name = doc.createElement("Name")
# 给节点添加文本节点
name.appendChild(doc.createTextNode(u"光荣之路教育科技公司"))
ceo = doc.createElement('CEO')
ceo.appendChild(doc.createTextNode(u'吴总'))
# 将各叶子节点添加到父节点company中
# 然后将company添加到跟节点companys中
company.appendChild(name)
company.appendChild(ceo)
root.appendChild(company)
fp = open('e:\\company.xml', 'w',encoding='utf-8')
doc.writexml(fp, indent='', addindent='\t', newl='\n', encoding="utf-8")
fp.close()
#coding=utf-8
import xml.dom.minidom
#在内存中创建一个空的文档
doc = xml.dom.minidom.Document()
#创建一个根节点Managers对象
root = doc.createElement('Managers')
#设置根节点的属性
root.setAttribute('company', 'xx科技')
root.setAttribute('address', '科技软件园')
#将根节点添加到文档对象中
doc.appendChild(root)
managerList = [{'name' : 'joy', 'age' : 27, 'sex' : '女'},
{'name' : 'tom', 'age' : 30, 'sex' : '男'},
{'name' : 'ruby', 'age' : 29, 'sex' : '女'} ]
for i in managerList :
nodeManager = doc.createElement('manager')
nodeName = doc.createElement('name')
#给叶子节点name设置一个文本节点,用于显示文本内容
nodeName.appendChild(doc.createTextNode(str(i['name'])))
nodeAge = doc.createElement("age")
nodeAge.appendChild(doc.createTextNode(str(i["age"])))
nodeSex = doc.createElement("sex")
nodeSex.appendChild(doc.createTextNode(str(i["sex"])))
#将各叶子节点添加到父节点Manager中,
#最后将Manager添加到根节点Managers中
nodeManager.appendChild(nodeName)
nodeManager.appendChild(nodeAge)
nodeManager.appendChild(nodeSex)
root.appendChild(nodeManager)
#开始写xml文档
fp = open('D:\\up\\1202\\company1.xml', 'w',encoding='utf-8')
doc.writexml(fp, indent='', addindent='\t', newl='\n', encoding="utf-8")
fp.close()