模块urllib requests json xml configparser 学习笔记

发起http请求 获取返回值 返回值是字符串

第三方模块安装 pip install requests

返回值格式 xml  html  jaon

 json 

功能  loads   字符串》》》列表 字典 字符串里面必须是“”,因为其他的编程语言字符串都是“”

       dups      列表》》》字符串

xml

from xml.etree import ElementTree as ET
tree = ET.parse('1.xml')
root = tree.getroot()
print(type(root))
#   root  <class 'xml.etree.ElementTree.Element'>
# dir(Element):
# append', 'attrib', 'clear', 'extend', 'find', 'findall', 'findtext', 'get', 'getchildren', 'getiterator', 'insert', 'items', 'iter', 'iterfind', 'itertext', 'keys', 'makeelement', 'remove', 'set', 'tag', 'tail', 'text'

 

 

from xml.etree import ElementTree as ET
tree = ET.parse('1.xml')
#ET.parse(添加文件路径)  ET.Element(文件节点,Element对象)

root = tree.getroot() son = root.makeelement("dat",{"k1":"k2"}) root.append(son) s = son.makeelement("tt",{"ks":"vs"}) son.append(s) tree.write("1.xml")
#添加子节点 孙子节点

添加子节点 孙子节点

from xml.etree import ElementTree as ET
tree = ET.parse('1.xml')
root = tree.getroot()
son = ET.Element("dat",{"k1":"k2"})
root.append(son)

tree.write("1.xml")
#直接根据类Element创建节点

直接根据类Element创建节点

 

from xml.etree import ElementTree as ET
tree = ET.parse('1.xml')
print(tree,type(tree))
from xml.etree.ElementTree import ElementTree  #用pychrm查找类ElementTree
#1   方法paese就是 调用ElementTree类 创建对象
#2 方法getroot 获取xml跟节点
#3 方法write 将创建的在内存的子节点 写入文件

 

#创建XML文档1
from xml.etree import ElementTree as ET
root = ET.Element("famliy")
son1 = ET.Element('son',{'name':"er1"})
son2 = ET.Element('son',{'name':"er2"})
root.append(son1)
root.append(son2)
sun1 = ET.Element('sun',{'name':"s1"})
sun2 = ET.Element('sun',{'name':"s2"})
son1.append(sun1)
son2.append(sun2)
tree = ET.ElementTree(root)
tree.write('1.xml')
# 创建XML文档2
from xml.etree import ElementTree as ET

root = ET.Element('famliy')
son1 = ET.SubElement(root,'sun',attrib={'name':"s1"})
son2 = ET.SubElement(root,'sun',attrib={'name':"s2"})
root.append(son1)
root.append(son2)
tree = ET.ElementTree(root)
tree.write('2.xml',encoding='utf-8')
# 创建XML文档3
from xml.etree import ElementTree as ET

# 创建根节点
root = ET.Element("famliy")


# 创建大儿子
# son1 = ET.Element('son', {'name': '儿1'})
son1 = root.makeelement('son', {'name': '儿1'})
# 创建小儿子
# son2 = ET.Element('son', {"name": '儿2'})
son2 = root.makeelement('son', {"name": '儿2'})

# 在大儿子中创建两个孙子
# grandson1 = ET.Element('grandson', {'name': '儿11'})
grandson1 = son1.makeelement('grandson', {'name': '儿11'})
# grandson2 = ET.Element('grandson', {'name': '儿12'})
grandson2 = son1.makeelement('grandson', {'name': '儿12'})

son1.append(grandson1)
son1.append(grandson2)


# 把儿子添加到根节点中
root.append(son1)
root.append(son1)

tree = ET.ElementTree(root)
tree.write('oooo.xml',encoding='utf-8', short_empty_elements=False)

创建方式(二)

 

#带缩进的创建xml 
from xml.etree import ElementTree as ET

root = ET.Element('famliy')
son1 = ET.SubElement(root,'sun',attrib={'name':"s1"})
son2 = ET.SubElement(root,'sun',attrib={'name':"s2"})
root.append(son1)
root.append(son2)
def MyWrite(root,name):
    from xml.dom import minidom
    rough_string = ET.tostring(root, 'utf-8')
    reparsed = minidom.parseString(rough_string)
    new_str = reparsed.toprettyxml(indent="\t")

    f = open(name, 'w', encoding='utf-8')
    f.write(new_str)
    f.close()

MyWrite(root,"5.xml")

configparser用于处理特定格式的文件

特定格式
# 注释1
;  注释2
 
[section1] # 节点
k1 = v1    # 值
k2:v2       # 值
 
[section2] # 节点
k1 = v1    # 值
import configparser
con = configparser.ConfigParser()
con.read('in',encoding='utf-8')
result = con.sections()
print(result)
ret = con.options('section1')
print(ret)
posted @ 2017-11-20 22:24  口水哥  阅读(365)  评论(0编辑  收藏  举报