当前位置:
首页 > Python基础教程 >
-
python基础教程之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()
栏目列表
最新更新
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
Python初学者友好丨详解参数传递类型
如何有效管理爬虫流量?
SQL SERVER中递归
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
这是目前我见过最好的跨域解决方案!
减少回流与重绘
减少回流与重绘
如何使用KrpanoToolJS在浏览器切图
performance.now() 与 Date.now() 对比
一款纯 JS 实现的轻量化图片编辑器
关于开发 VS Code 插件遇到的 workbench.scm.
前端设计模式——观察者模式
前端设计模式——中介者模式
创建型-原型模式