day22-python之模块

1.os

 1 import os
 2 
 3 # print(os.getcwd())
 4 # os.chdir("..")
 5 # print(os.getcwd())
 6 
 7 # os.makedirs('dirname1/dirname2')
 8 # os.removedirs("dirname1/dirname2")
 9 # print(os.listdir())
10 # print(os.stat("test.py"))
11 
12 # print(os.system("dir"))
13 #
14 # print(os.path.split(r"C:\Users\Administrator\脱产三期\day22\sss.py"))
15 # print(os.path.dirname(r"C:\Users\Administrator\脱产三期\day22\sss.py"))
16 # print(os.path.basename(r"C:\Users\Administrator\脱产三期\day22\sss.py"))
17 a="C:\Users\Administrator"
18 b="脱产三期\day22\sss.py"
19 #
20 os.path.join(a,b)#  路径拼接

2.xml解析

 1 # import xml.etree.ElementTree as ET
 2 
 3 
 4 
 5 # tree = ET.parse("../day22/xml_lesson.xml")
 6 # root = tree.getroot()
 7 # print(root.tag)
 8 
 9 
10 # for i in root:
11 #
12 #     # print(i.tag)
13 #     # print(i.attrib)
14 #     for j in i:
15 #         # print(j.tag)
16 #         # print(j.attrib)
17 #         print(j.text)
18 
19 
20 
21 
22 # # 遍历xml文档
23 # for child in root:
24 #     # print(child.tag, child.attrib)
25 #     for i in child:
26 #         print(i.tag, i.text)
27 
28 # 只遍历year 节点
29 # for node in root.iter('year'):
30 #     print(node.tag, node.text)
31 # # ---------------------------------------
32 
33 # import xml.etree.ElementTree as ET
34 #
35 # tree = ET.parse("../day22/xml_lesson.xml")
36 # root = tree.getroot()
37 
38 # 修改
39 # for node in root.iter('year'):
40 #     new_year = int(node.text) + 1
41 #     node.text = str(new_year)
42 #     node.set("updated", "no")
43 # #
44 # tree.write("../day22/xml_lesson.xml")
45 #
46 # 删除node
47 # for country in root.findall('country'):
48 #     rank = int(country.find('rank').text)
49 #     if rank > 50:
50 #         root.remove(country)
51 #
52 # tree.write('output.xml')
53 
54 
55 import xml.etree.ElementTree as ET
56 
57 new_xml = ET.Element("namelist")
58 #
59 name = ET.SubElement(new_xml, "name", attrib={"enrolled": "yes"})
60 age = ET.SubElement(name, "age", attrib={"checked": "no"})
61 sex = ET.SubElement(name, "sex")
62 sex.text = '33'
63 name2 = ET.SubElement(new_xml, "name", attrib={"enrolled": "no"})
64 age = ET.SubElement(name2, "age")
65 age.text = '19'
66 
67 
68 
69 et = ET.ElementTree(new_xml)  # 生成文档对象
70 et.write("test1.xml", encoding="utf-8", xml_declaration=True)
71 
72 ET.dump(new_xml)  # 打印生成的格式

3.json & pickle

 1 # dic='{"name":"alex"}'
 2 # f=open("hello","w")
 3 # f.write(dic)
 4 #
 5 # f_read=open("hello","r")
 6 # data=f_read.read()
 7 # print(type(data))
 8 # data=eval(data)
 9 # print(data["name"])
10 
11 import json
12 
13 
14 # dic={'name':'alex'}#---->{"name":"alex"}----->'{"name":"alex"}'
15 # i=8                 #---->'8'
16 # s='hello'          #---->"hello"------>'"hello"'
17 # l=[11,22]           #---->"[11,22]"
18 # #
19 # f=open("new_hello","w")
20 #
21 # dic_str=json.dumps(dic)
22 # f.write(dic_str)    #json.dump(dic,f)
23 
24 
25 
26 # f_read=open("new_hello","r")
27 # data=json.loads(f_read.read())      # data=json.load(f)
28 #
29 # #
30 # print(data["name"])
31 # print(data)
32 # print(type(data))
33 
34 # print(s)
35 # print(type(s))
36 
37 
38 # data=json.dumps(dic)
39 
40 # print(data)     #{"name": "alex"}
41 # print(type(data))
42 
43 
44 #注意:
45 # import json
46 #
47 # with open("hello","r") as f:
48 #     data=f.read()
49 #     data=json.loads(data)
50 #     print(data["name"])
51 
52 #----------------------pickle-------
53 # import pickle
54 
55 # dic = {'name': 'alvin', 'age': 23, 'sex': 'male'}
56 
57 # print(type(dic))  # <class 'dict'>
58 #
59 # j = pickle.dumps(dic)
60 # print(type(j))  # <class 'bytes'>
61 #
62 # f = open('../day22/序列化对象_pickle', 'wb')  # 注意是w是写入str,wb是写入bytes,j是'bytes'
63 # f.write(j)  # -------------------等价于pickle.dump(dic,f)
64 #
65 # f.close()
66 # # -------------------------反序列化
67 # import pickle
68 #
69 # f = open('../day22/序列化对象_pickle', 'rb')
70 # #
71 # data = pickle.loads(f.read())  # 等价于data=pickle.load(f)
72 #
73 # print(data['age'])
74 # # -------------------------shelve模块---------
75 import shelve
76 #
77 f = shelve.open(r'shelve1.bak')  # 目的:将一个字典放入文本 f={}
78 #
79 f['stu1_info']={'name':'alex','age':'18'}
80 f['stu2_info']={'name':'alvin','age':'20'}
81 f['school_info']={'website':'oldboyedu.com','city':'beijing'}
82 f.close()
83 
84 print(f.get('stu1_info')['age'])
85 
86 
87 
88 # dic={}
89 #
90 # dic["name"]="alvin"
91 # dic["info"]={"name":"alex"}

4.eval

1 print(eval("12+(34*6+2-5*(2-1))"))

5.sys

 1 import sys
 2 # # print(sys.argv)
 3 #
 4 # command=sys.argv[1]
 5 # path=sys.argv[2]
 6 #
 7 # if command=="post":
 8 #     pass
 9 #
10 # elif command=="get":
11 #     pass
12 #
13 import time
14 for i in range(100):
15     sys.stdout.write("#")
16     time.sleep(0.1)
17     sys.stdout.flush()

猜你喜欢

转载自www.cnblogs.com/sqy-yyr/p/10899166.html
今日推荐