python常用模块学习2

#sys模块
import sys
#
# print(sys.argv)#命令行参数List,第一个元素是程序本身路径
#主要用作网络请求判断
# command=sys.argv[1]
# path=sys.argv[2]
#
# if command=="post":
#     pass
#
#
# elif command=="download":
#     pass
#

#向屏幕显示相应内容
import time
for i in range(100):

    sys.stdout.write("#")
    time.sleep(0.1)
    sys.stdout.flush()#屏幕刷新
    #sys.stdout.write("#")


#shelve模块
import shelve
f=shelve.open(r"shelve")#目的:将一个字典放入文本 f={}
f["study"]={"name":"alex","age":19}
print(f.get("study")["name"])
f.close()

# dic={}
# dic["name"]="alive"
# dic["info"]={"kskwkk"}
# print(dic)
#
# f=shelve.open(r"shelve")
# print(f.get("study")['age'])
# f.close()


#xml模块
# import xml.etree.ElementTree as ET
#
#
# #parse 解析xml数据
# tree=ET.parse("xml_test")
#
# root=tree.getroot()
#print(root.tag)#获取根节点便签名

#遍历xml文档
# for i in root:#遍历根节点
# #print(i.tag)#打印标签名
#
# #print(i.attrib)#打印标签属性
#
# print(i.text)#打印标签内容


#只遍历指定节点
# for node in root.iter("body"):
# print(node.tag,node.text)


#修改
# for node in root.iter("body"):
# new_about=node.text+"Hello"
# node.text=new_about
# node.set("uplode","img")
# tree.write("xml_test1.xml")

#删除node
# for node in root.findall("huby"):
# root.remove(node)
# tree.write("xml_test1.xml")


#生成一个xml数据
import xml.etree.ElementTree as ET
new_xml=ET.Element("namelist")#创建根节点

'''
<namelist>
<name enrolad="yes">
<age chcecked="no"></age>
<sex>33</sex>
</name>

</namelist>

'''


name=ET.SubElement(new_xml,"name",attrib={"enrolad":"yes"})#插入子元素
age=ET.SubElement(name,"age",attrib={"checked":"no"})
age.text=str(20)
sex=ET.SubElement(name,"sex")
sex.text="33"

et=ET.ElementTree(new_xml)#生成文档对象
et.write("test.xml",encoding="utf-8",xml_declaration=True)

 
如果我失败了,至少我尝试过,不会因为痛失机会而后悔
原文地址:https://www.cnblogs.com/tangcode/p/11201693.html