模块安装方式:
安装模块 pip3 install xxx
源码安装 CD到安装目录 输入 python setup.py install
模块分为三种:
- 自定义模块
- 第三方模块
- 内置模块
|
1
2
3
4
|
import modulefrom module.xx.xx import xxfrom module.xx.xx import xx as rename from module.xx.xx import * |
模块查找顺序:
C:\Users\Administrator\python36\Lib\idlelib
C:\Users\Administrator\python36\python36.zip
C:\Users\Administrator\python36\DLLs
C:\Users\Administrator\python36\lib 内置目录
C:\Users\Administrator\python36
C:\Users\Administrator\python36\lib\site-packages 第三方模块安装目录
通过sys.path 添加路径
sys.path.append(‘D:‘) 里面为路径名
====================================================
载入模块 import time
1 time.sleep(x) #延迟x秒。 2 time.time()# 时间戳,无参数,返回一个<built-in function time>1970年1月1日之后的秒 3 time.ctime() #'Sat Sep 9 21:59:55 2017' 返回当前时间点 4 time.ctime(time.time()-86400) # 'Fri Sep 8 22:05:11 2017' 返回昨日当前的时间 5 time.gmtime() # time.struct_time(tm_year=2017, tm_mon=9, tm_mday=9, tm_hour=14, tm_min=6, tm_sec=50, tm_wday=5, tm_yday=252, tm_isdst=0) #得到散落的值。 6 print(str(time_obj.tm_year)+'-'+ str(time_obj.tm_mon)+'-'+str(time_obj.tm_mday)) # 拼接得到数值'2017-9-9' 7 #################简单的拼接 8 time.strftime('%Y-%m-%d') #默认当前时间日期 9 time.strftime('%Y-%m-%d',time.localtime()) #默认当前时间 10 time.asctime() 'Sat Sep 9 22:23:27 2017' #获取当前时间 11 time.strptime('2014-11-11', '%Y-%m-%d') 12 #time.struct_time(tm_year=2014, tm_mon=11, tm_mday=11, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=1, tm_yday=315, tm_isdst=-1) 将制定时间拆分
13 time.mktime(time.localtime()) 将时间对象转化相应的时间戳,里面必须有参数#1505007004.0
14 time.localtime() #取本地时间
15 print(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())) #2017-09-10 09:38:03 格式化本地时间

import datetime
print(datetime.datetime.now()) #2017-09-10 09:56:35.514159 print (datetime.datetime.now() - datetime.timedelta(days=5)) #在原有天数加某天
载入模块 import sys
1 sys.argv 命令行参数List,第一个元素是程序本身路径
2 sys.path 返回模块的搜索路径,初始化时使用PYTHONPATH环境变量的值 3 sys.exit(n) 退出程序,正常退出时exit(0) 4 sys.version 获取Python解释程序的版本信息 5 sys.maxsize) #2147483647 6 sys.platform 返回操作系统平台名称 #win32 7 sys.stdout.write('please:') #往显示器打文字,通常一行显示 8 val = sys.stdin.readline() #[:-1] 等待用户在屏幕输入,读取
进度条打印
1 import sys,time,datetime 2 for i in range(31): 3 sys.stdout.write('\r')#每一次清空原行 4 sys.stdout.write('%s%% | %s'%(int(i/30*100),int(i/30*100)*'*')) 5 #print('%s%% | %s'%(int(i/30*100),int(i/30*100)*'*')) 换行打印 6 sys.stdout.flush()#强制刷新屏幕 7 time.sleep(0.5)
import pickle
python的pickle模块实现了python的所有数据序列和反序列化。基本上功能使用和JSON模块没有太大区别,方法也同样是dumps/dump和loads/load。cPickle是pickle模块的C语言编译版本相对速度更快。
与JSON不同的是pickle不是用于多种语言间的数据传输,它仅作为python对象的持久化或者python程序间进行互相传输对象的方法,因此它支持了python所有的数据类型。
json只适应于各大交互器的列表,字典等进行序列化和反序列化,pickle只针对于python各字符串进行处理,相对pickle更强大
常用购物车写入数据库存储
1 import pickle 2 accounts={ 3 1000:{ 4 'name':'Alex LI', 5 'E-mainl':'8******@**.com', 6 'password':'abc123', 7 'balance':'15000', 8 'phone':'123455687', 9 'bank_acc':{ 10 'ICBC':'423523523', 11 'CBC':'32432532623', 12 'ABC':'325u987hj' 13 } 14 }, 15 1001: { 16 'name': 'vbkw LI', 17 'E-mainl': '8******@**.com', 18 'password': 'dsabc123', 19 'balance': '158000', 20 'phone': 'd123455687', 21 'bank_acc': { 22 'ICBC': 'df423523523', 23 'CBC': 'gsd32432532623', 24 'ABC': 'sd325u987hj' 25 } 26 } 27 28 } 29 30 with open('account.db','wb') as f: 31 account = pickle.dumps(accounts) 32 #print(account_dic[1000]['balance']) 33 f.write(account) 34 f.close() 35 #print('wanchneg ') 36 #========================================================= 37 import pickle 38 with open('account.db','rb') as f: 39 account_dic=pickle.loads(f.read()) 40 f.close() 41 account_dic[1000]['balance']-=500 42 43 with open('account.db','wb') as r: 44 r.write(pickle.dumps(account_dic)) 45 f.close()
JSON和pickle模块的区别
1、JSON只能处理基本数据类型。将字典转化为字符串是 里面必须是双引号,字符串边上是隐藏的打印。pickle能处理所有Python的数据类型。
2、JSON用于各种语言之间的字符转换。pickle用于Python程序对象的持久化或者Python程序间对象网络传输,但不同版本的Python序列化可能还有差异。
Python中用于序列化的两个模块
- json 用于【字符串】和 【python基本数据类型】 间进行转换
- pickle 用于【python特有的类型】 和 【python基本数据类型】间进行转换
Json模块提供了四个功能:dumps、dump、loads、load
pickle模块提供了四个功能:dumps、dump、loads、load
import json
s='{"data":{"yesterday":{"date":"ere"},"sd":"ssv"}}' request=json.loads(s) print(request,type(request))
import re
re.match(pattern, string, flags=0): 从头开始匹配
re.search(self, terms, operator=None): 查找全部字符串,返回匹配的第一个字符串
re.findall(pattern, string, flags=0) 匹配全部字符,以列表的形式储存
re.sub(pattern, repl, string, count=0, flags=0)
|
1
2
3
4
5
6
7
8
|
# sub,替换匹配成功的指定位置字符串sub(pattern, repl, string, count=0, flags=0)# pattern: 正则模型# repl : 要替换的字符串或可执行对象# string : 要匹配的字符串# count : 指定匹配个数# flags : 匹配模式origin = "hello alex bcd alex lge alex acd 19"
r = re.sub("a\w+", "999", origin, 2)
print(r)
|
re.split(pattern, string, maxsplit=0, flags=0
|
1
2
3
4
5
6
7
|
# split,根据正则匹配分割字符串split(pattern, string, maxsplit=0, flags=0)# pattern: 正则模型# string : 要匹配的字符串# maxsplit:指定分割个数# flags : 匹配模式# 无分组
origin = "hello alex bcd alex lge alex acd 19"
r = re.split("alex", origin, 1)
print(r)
# 有分组
origin = "hello alex bcd alex lge alex acd 19"
r1 = re.split("(alex)", origin, 1)
print(r1)
r2 = re.split("(al(ex))", origin, 1)
print(r2)
|
import os
os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径 os.chdir("dirname") 改变当前脚本工作目录;相当于shell下cd os.curdir 返回当前目录: ('.') os.pardir 获取当前目录的父目录字符串名:('..') os.makedirs('dir1/dir2') 可生成多层递归目录 os.removedirs('dirname1') 若目录为空,则删除,并递归到上一级目录,如若也为空,则删除,依此类推 os.mkdir('dirname') 生成单级目录;相当于shell中mkdir dirname os.rmdir('dirname') 删除单级空目录,若目录不为空则无法删除,报错;相当于shell中rmdir dirname os.listdir('dirname') 列出指定目录下的所有文件和子目录,包括隐藏文件,并以列表方式打印 os.remove() 删除一个文件 os.rename("oldname","new") 重命名文件/目录 os.stat('path/filename') 获取文件/目录信息 os.sep 操作系统特定的路径分隔符,win下为"\\",Linux下为"/" os.linesep 当前平台使用的行终止符,win下为"\t\n",Linux下为"\n" os.pathsep 用于分割文件路径的字符串 os.name 字符串指示当前使用平台。win->'nt'; Linux->'posix' os.system("bash command") 运行shell命令,直接显示 os.environ 获取系统环境变量 os.path.abspath(path) 返回path规范化的绝对路径 os.path.split(path) 将path分割成目录和文件名二元组返回 os.path.dirname(path) 返回path的目录。其实就是os.path.split(path)的第一个元素 os.path.basename(path) 返回path最后的文件名。如何path以/或\结尾,那么就会返回空值。即os.path.split(path)的第二个元素 os.path.exists(path) 如果path存在,返回True;如果path不存在,返回False os.path.isabs(path) 如果path是绝对路径,返回True os.path.isfile(path) 如果path是一个存在的文件,返回True。否则返回False os.path.isdir(path) 如果path是一个存在的目录,则返回True。否则返回False os.path.join(path1[, path2[, ...]]) 将多个路径组合后返回,第一个绝对路径之前的参数将被忽略 os.path.getatime(path) 返回path所指向的文件或者目录的最后存取时间 os.path.getmtime(path) 返回path所指向的文件或者目录的最后修改时间
import hashlib
用于加密相关的操作,代替了md5模块和sha模块,主要提供 SHA1, SHA224, SHA256, SHA384, SHA512 ,MD5 算法,一般使用MD5,算法不容易破解
import hashlib hash=hashlib.md5(bytes('jhkhugesdf:erwt',encoding='utf-8'))#c0a17a8d7200f0556684f8b05afa4df3 可空 hash.update(bytes('admin',encoding='utf-8')) print(hash.hexdigest()) #202cb962ac59075b964b07152d234b70
python内置还有一个 hmac 模块,它内部对我们创建 key 和 内容 进行进一步的处理然后再加密
|
1
2
3
4
5
|
import hmach = hmac.new(bytes('898oaFs09f',encoding="utf-8"))h.update(bytes('admin',encoding="utf-8"))print(h.hexdigest()) |
python 用于MD5加密密码是几登入例子
1 import hashlib 2 def md5(arg): 3 hash=hashlib.md5()#c0a17a8d7200f0556684f8b05afa4df3 可空 4 hash.update(bytes(arg,encoding='utf-8')) 5 return hash.hexdigest() #202cb962ac59075b964b07152d234b70 6 7 def login(uwer,pwed): 8 with open('sjx.db', 'r', encoding='utf-8') as f: 9 for line in f: 10 u,p=line.strip().split('|') 11 if u==uwer and p==md5(pwd): 12 f.close() 13 return True 14 def registet(uwer,pwd): 15 with open('sjx.db','a',encoding='utf-8') as f: 16 temp=uwer+'|' +md5(pwd) 17 f.write(temp) 18 f.close() 19 20 i=input('1、登入,2、注册') 21 if i=='2': 22 uwer=input('用户名:') 23 pwd=input('密码:') 24 registet(uwer,pwd) 25 elif i=='1': 26 uwer = input('用户名:') 27 pwd = input('密码:') 28 r=login(uwer,pwd) 29 if r: 30 print('登入成功') 31 else: 32 print('登入失败')
字符串格式化:
Python的字符串格式化有两种方式: 百分号方式、format方式。
百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存
百分号方式 %[(name)][flags][width].[precision]typecode 中间参数可有可无。
百分号用法:
(name) 可选,用于选择指定的key
flags 可选,可供选择的值有:
+ 右对齐;正数前加正好,负数前加负号;
- 左对齐;正数前无符号,负数前加负号;
空格 右对齐;正数前加空格,负数前加负号;
0 右对齐;正数前无符号,负数前加负号;用0填充空白处
width 可选,占有宽度
.precision 可选,小数点后保留的位数
typecode 必选
s,获取传入对象的__str__方法的返回值,并将其格式化到指定位置
r,获取传入对象的__repr__方法的返回值,并将其格式化到指定位置
c,整数:将数字转换成其unicode对应的值,10进制范围为 0 <= i <= 1114111(py27则只支持0-255);字符:将字符添加到指定位置
o,将整数转换成 八 进制表示,并将其格式化到指定位置
x,将整数转换成十六进制表示,并将其格式化到指定位置
d,将整数、浮点数转换成 十 进制表示,并将其格式化到指定位置
e,将整数、浮点数转换成科学计数法,并将其格式化到指定位置(小写e)
E,将整数、浮点数转换成科学计数法,并将其格式化到指定位置(大写E)
f, 将整数、浮点数转换成浮点数表示,并将其格式化到指定位置(默认保留小数点后6位)
F,同上
g,自动调整将整数、浮点数转换成 浮点型或科学计数法表示(超过6位数用科学计数法),并将其格式化到指定位置(如果是科学计数则是e;)
G,自动调整将整数、浮点数转换成 浮点型或科学计数法表示(超过6位数用科学计数法),并将其格式化到指定位置(如果是科学计数则是E;)
%,当字符串中存在格式化标志时,需要用 %%表示一个百分号
>>> s='I nam %(n)010sbidden ,age%(t)d'%{'n':'bottom','t':18} >>> s 'I nam bottombidden ,age18' #右对齐 s='I nam %(n)s ,age%(t)d'%{'n':'bottom','t':18} print(s) 'I nam bottom ,age18' #常用方法,也可以直接省略
>>> s='I nam %(n)010sbidden ,age%(t).2f'%{'n':'bottom','t':18}
>>> s
'I nam bottombidden ,age18.00' precision .xf 保留几位小数
常见格式化
tpl = "i am %s" % "alex" tpl = "i am %s age %d" % ("alex", 18) tpl = "i am %(name)s age %(age)d" % {"name": "alex", "age": 18} tpl = "percent %.2f" % 99.97623 tpl = "i am %(pp).2f" % {"pp": 123.425556, } tpl = "i am %.2f %%" % {"pp": 123.425556, }
2、Format方式
[[fill]align][sign][#][0][width][,][.precision][type]
fill 【可选】空白处填充的字符 align 【可选】对齐方式(需配合width使用) <,内容左对齐 >,内容右对齐(默认) =,内容右对齐,将符号放置在填充字符的左侧,且只对数字类型有效。 即使:符号+填充物+数字 ^,内容居中 sign 【可选】有无符号数字 +,正号加正,负号加负; -,正号不变,负号加负; 空格 ,正号空格,负号加负; # 【可选】对于二进制、八进制、十六进制,如果加上#,会显示 0b/0o/0x,否则不显示 , 【可选】为数字添加分隔符,如:1,000,000 width 【可选】格式化位所占宽度 .precision 【可选】小数位保留精度 type 【可选】格式化类型 传入” 字符串类型 “的参数 s,格式化字符串类型数据 空白,未指定类型,则默认是None,同s 传入“ 整数类型 ”的参数 b,将10进制整数自动转换成2进制表示然后格式化 c,将10进制整数自动转换为其对应的unicode字符 d,十进制整数 o,将10进制整数自动转换成8进制表示然后格式化; x,将10进制整数自动转换成16进制表示然后格式化(小写x) X,将10进制整数自动转换成16进制表示然后格式化(大写X) 传入“ 浮点型或小数类型 ”的参数 e, 转换为科学计数法(小写e)表示,然后格式化; E, 转换为科学计数法(大写E)表示,然后格式化; f , 转换为浮点型(默认小数点后保留6位)表示,然后格式化; F, 转换为浮点型(默认小数点后保留6位)表示,然后格式化; g, 自动在e和f中切换 G, 自动在E和F中切换 %,显示百分比(默认显示小数点后6位)
^、<、>分别是居中、左对齐、右对齐,后面带宽度
:号后面带填充的字符,只能是一个字符,不指定的话默认是用空格填充
比如
|
1
2
3
4
5
6
|
In [15]: '{:>8}'.format('189')Out[15]: ' 189'In [16]: '{:0>8}'.format('189')Out[16]: '00000189'In [17]: '{:a>8}'.format('189')Out[17]: 'aaaaa189' |
b、d、o、x分别是二进制、十进制、八进制、十六进制。
| 数字 | 格式 | 输出 | 描述 |
| 3.1415926 | {:.2f} | 3.14 | 保留小数点后两位 |
| 3.1415926 | {:+.2f} | 3.14 | 带符号保留小数点后两位 |
| -1 | {:+.2f} | -1 | 带符号保留小数点后两位 |
| 2.71828 | {:.0f} | 3 | 不带小数 |
| 1000000 | {:,} | 1,000,000 | 以逗号分隔的数字格式 |
| 0.25 | {:.2%} | 25.00% | 百分比格式 |
| 1000000000 | {:.2e} | 1.00E+09 | 指数记法 |
| 25 | {0:b} | 11001 | 转换成二进制 |
| 25 | {0:d} | 25 | 转换成十进制 |
| 25 | {0:o} | 31 | 转换成八进制 |
| 25 | {0:x} | 19 | 转换成十六进制 |
三 对齐与填充
| 数字 | 格式 | 输出 | 描述 |
| 5 | {:0>2} | 05 | 数字补零 (填充左边, 宽度为2) |
| 5 | {:x<4} | 5xxx | 数字补x (填充右边, 宽度为4) |
| 10 | {:x^4} | x10x | 数字补x (填充右边, 宽度为4) |
| 13 | {:10} | 13 | 右对齐 (默认, 宽度为10) |
| 13 | {:<10} | 13 | 左对齐 (宽度为10) |
| 13 | {:^10} | 13 | 中间对齐 (宽度为10) |
四 其他
1.转义{和}符号
|
1
|
print '{{ hello {0} }}'.format('Kevin')
|
跟%中%%转义%一样,formate中用两个大括号来转义
常见格式化
1 tpl = "i am {}, age {}, {}".format("seven", 18, 'alex') #'i am seven, age 18, alex' 2 3 tpl = "i am {}, age {}, {}".format(*["seven", 18, 'alex']) 4 5 tpl = "i am {0}, age {1}, really {0}".format("seven", 18) 6 7 tpl = "i am {0}, age {1}, really {0}".format(*["seven", 18]) 8 9 tpl = "i am {name}, age {age}, really {name}".format(name="seven", age=18) 10 11 tpl = "i am {name}, age {age}, really {name}".format(**{"name": "seven", "age": 18}) 12 13 tpl = "i am {0[0]}, age {0[1]}, really {0[2]}".format([1, 2, 3], [11, 22, 33]) 14 15 tpl = "i am {:s}, age {:d}, money {:f}".format("seven", 18, 88888.1) 16 17 tpl = "i am {:s}, age {:d}".format(*["seven", 18]) 18 19 tpl = "i am {name:s}, age {age:d}".format(name="seven", age=18) 20 21 tpl = "i am {name:s}, age {age:d}".format(**{"name": "seven", "age": 18}) 22 23 tpl = "numbers: {:b},{:o},{:d},{:x},{:X}, {:%}".format(15, 15, 15, 15, 15, 15.87623, 2) 24 25 tpl = "numbers: {:b},{:o},{:d},{:x},{:X}, {:%}".format(15, 15, 15, 15, 15, 15.87623, 2) 26 27 tpl = "numbers: {0:b},{0:o},{0:d},{0:x},{0:X}, {0:%}".format(15) 28 29 tpl = "numbers: {num:b},{num:o},{num:d},{num:x},{num:X}, {num:%}".format(num=15)
模块拾忆——----------------------------
__doc__ py #文件的注释
__file__ 本身路径 #
__package__ # 当前文件.py所在文件夹下的包,用. 划分
__cahed__ #缓存
__name__ #只有执行python X.py时候,__name_=‘main’,否则等于模块名。
>>> from bs4 import BeautifulSoup >>> print(BeautifulSoup.__name__) BeautifulSoup
__builtins__ 内置函数再这个模块里面
XML
XML格式类型是节点嵌套节点,对于每一个节点均有以下功能,以便对当前节点进行操作:
XML格式
<data>
<country name="Liechtenstein">
<rank updated="yes">2</rank>
<year>2023</year>
<gdppc>141100</gdppc>
<neighbor direction="E" name="Austria" />
<neighbor direction="W" name="Switzerland" />
</country>
<country name="Singapore">
<rank updated="yes">5</rank>
<year>2026</year>
<gdppc>59900</gdppc>
<neighbor direction="N" name="Malaysia" />
</country>
<country name="Panama">
<rank updated="yes">69</rank>
<year>2026</year>
<gdppc>13600</gdppc>
<neighbor direction="W" name="Costa Rica" />
<neighbor direction="E" name="Colombia" />
</country>
</data>
解析XML格式
将字符串解析成XML对象
1 from xml.etree import ElementTree as ET 2 3 4 # 打开文件,读取XML内容 5 str_xml = open('xo.xml', 'r').read() 6 7 # 将字符串解析成xml特殊对象,root代指xml文件的根节点 8 root = ET.XML(str_xml)
将文件直接解析成XML对象
from xml.etree import ElementTree as ET # 直接解析xml文件 tree = ET.parse("xo.xml") # 获取xml文件的根节点 root = tree.getroot()
操作XML
XML格式类型是节点嵌套节点,对于每一个节点均有以下功能,以便对当前节点进行操作:
class Element: """An XML element. This class is the reference implementation of the Element interface. An element's length is its number of subelements. That means if you want to check if an element is truly empty, you should check BOTH its length AND its text attribute. The element tag, attribute names, and attribute values can be either bytes or strings. *tag* is the element name. *attrib* is an optional dictionary containing element attributes. *extra* are additional element attributes given as keyword arguments. Example form: <tag attrib>text<child/>...</tag>tail """ 当前节点的标签名 tag = None """The element's name.""" 当前节点的属性 attrib = None """Dictionary of the element's attributes.""" 当前节点的内容 text = None """ Text before first subelement. This is either a string or the value None. Note that if there is no text, this attribute may be either None or the empty string, depending on the parser. """ tail = None """ Text after this element's end tag, but before the next sibling element's start tag. This is either a string or the value None. Note that if there was no text, this attribute may be either None or an empty string, depending on the parser. """ def __init__(self, tag, attrib={}, **extra): if not isinstance(attrib, dict): raise TypeError("attrib must be dict, not %s" % ( attrib.__class__.__name__,)) attrib = attrib.copy() attrib.update(extra) self.tag = tag self.attrib = attrib self._children = [] def __repr__(self): return "<%s %r at %#x>" % (self.__class__.__name__, self.tag, id(self)) def makeelement(self, tag, attrib): 创建一个新节点 """Create a new element with the same type. *tag* is a string containing the element name. *attrib* is a dictionary containing the element attributes. Do not call this method, use the SubElement factory function instead. """ return self.__class__(tag, attrib) def copy(self): """Return copy of current element. This creates a shallow copy. Subelements will be shared with the original tree. """ elem = self.makeelement(self.tag, self.attrib) elem.text = self.text elem.tail = self.tail elem[:] = self return elem def __len__(self): return len(self._children) def __bool__(self): warnings.warn( "The behavior of this method will change in future versions. " "Use specific 'len(elem)' or 'elem is not None' test instead.", FutureWarning, stacklevel=2 ) return len(self._children) != 0 # emulate old behaviour, for now def __getitem__(self, index): return self._children[index] def __setitem__(self, index, element): # if isinstance(index, slice): # for elt in element: # assert iselement(elt) # else: # assert iselement(element) self._children[index] = element def __delitem__(self, index): del self._children[index] def append(self, subelement): 为当前节点追加一个子节点 """Add *subelement* to the end of this element. The new element will appear in document order after the last existing subelement (or directly after the text, if it's the first subelement), but before the end tag for this element. """ self._assert_is_element(subelement) self._children.append(subelement) def extend(self, elements): 为当前节点扩展 n 个子节点 """Append subelements from a sequence. *elements* is a sequence with zero or more elements. """ for element in elements: self._assert_is_element(element) self._children.extend(elements) def insert(self, index, subelement): 在当前节点的子节点中插入某个节点,即:为当前节点创建子节点,然后插入指定位置 """Insert *subelement* at position *index*.""" self._assert_is_element(subelement) self._children.insert(index, subelement) def _assert_is_element(self, e): # Need to refer to the actual Python implementation, not the # shadowing C implementation. if not isinstance(e, _Element_Py): raise TypeError('expected an Element, not %s' % type(e).__name__) def remove(self, subelement): 在当前节点在子节点中删除某个节点 """Remove matching subelement. Unlike the find methods, this method compares elements based on identity, NOT ON tag value or contents. To remove subelements by other means, the easiest way is to use a list comprehension to select what elements to keep, and then use slice assignment to update the parent element. ValueError is raised if a matching element could not be found. """ # assert iselement(element) self._children.remove(subelement) def getchildren(self): 获取所有的子节点(废弃) """(Deprecated) Return all subelements. Elements are returned in document order. """ warnings.warn( "This method will be removed in future versions. " "Use 'list(elem)' or iteration over elem instead.", DeprecationWarning, stacklevel=2 ) return self._children def find(self, path, namespaces=None): 获取第一个寻找到的子节点 """Find first matching element by tag name or path. *path* is a string having either an element tag or an XPath, *namespaces* is an optional mapping from namespace prefix to full name. Return the first matching element, or None if no element was found. """ return ElementPath.find(self, path, namespaces) def findtext(self, path, default=None, namespaces=None): 获取第一个寻找到的子节点的内容 """Find text for first matching element by tag name or path. *path* is a string having either an element tag or an XPath, *default* is the value to return if the element was not found, *namespaces* is an optional mapping from namespace prefix to full name. Return text content of first matching element, or default value if none was found. Note that if an element is found having no text content, the empty string is returned. """ return ElementPath.findtext(self, path, default, namespaces) def findall(self, path, namespaces=None): 获取所有的子节点 """Find all matching subelements by tag name or path. *path* is a string having either an element tag or an XPath, *namespaces* is an optional mapping from namespace prefix to full name. Returns list containing all matching elements in document order. """ return ElementPath.findall(self, path, namespaces) def iterfind(self, path, namespaces=None): 获取所有指定的节点,并创建一个迭代器(可以被for循环) """Find all matching subelements by tag name or path. *path* is a string having either an element tag or an XPath, *namespaces* is an optional mapping from namespace prefix to full name. Return an iterable yielding all matching elements in document order. """ return ElementPath.iterfind(self, path, namespaces) def clear(self): 清空节点 """Reset element. This function removes all subelements, clears all attributes, and sets the text and tail attributes to None. """ self.attrib.clear() self._children = [] self.text = self.tail = None def get(self, key, default=None): 获取当前节点的属性值 """Get element attribute. Equivalent to attrib.get, but some implementations may handle this a bit more efficiently. *key* is what attribute to look for, and *default* is what to return if the attribute was not found. Returns a string containing the attribute value, or the default if attribute was not found. """ return self.attrib.get(key, default) def set(self, key, value): 为当前节点设置属性值 """Set element attribute. Equivalent to attrib[key] = value, but some implementations may handle this a bit more efficiently. *key* is what attribute to set, and *value* is the attribute value to set it to. """ self.attrib[key] = value def keys(self): 获取当前节点的所有属性的 key """Get list of attribute names. Names are returned in an arbitrary order, just like an ordinary Python dict. Equivalent to attrib.keys() """ return self.attrib.keys() def items(self): 获取当前节点的所有属性值,每个属性都是一个键值对 """Get element attributes as a sequence. The attributes are returned in arbitrary order. Equivalent to attrib.items(). Return a list of (name, value) tuples. """ return self.attrib.items() def iter(self, tag=None): 在当前节点的子孙中根据节点名称寻找所有指定的节点,并返回一个迭代器(可以被for循环)。 """Create tree iterator. The iterator loops over the element and all subelements in document order, returning all elements with a matching tag. If the tree structure is modified during iteration, new or removed elements may or may not be included. To get a stable set, use the list() function on the iterator, and loop over the resulting list. *tag* is what tags to look for (default is to return all elements) Return an iterator containing all the matching elements. """ if tag == "*": tag = None if tag is None or self.tag == tag: yield self for e in self._children: yield from e.iter(tag) # compatibility def getiterator(self, tag=None): # Change for a DeprecationWarning in 1.4 warnings.warn( "This method will be removed in future versions. " "Use 'elem.iter()' or 'list(elem.iter())' instead.", PendingDeprecationWarning, stacklevel=2 ) return list(self.iter(tag)) def itertext(self): 在当前节点的子孙中根据节点名称寻找所有指定的节点的内容,并返回一个迭代器(可以被for循环)。 """Create text iterator. The iterator loops over the element and all subelements in document order, returning all inner text. """ tag = self.tag if not isinstance(tag, str) and tag is not None: return if self.text: yield self.text for e in self: yield from e.itertext() if e.tail: yield e.tail 节点功能一览表
Element 拾忆
from xml.etree import ElementTree as ET # 直接解析xml文件 tree = ET.parse("xo.xml") ET.Element('tt',{'kk','vv'}) #可以直接创建节点对象 # 获取xml文件的根节点 root = tree.getroot() print(root.tag,root.text,root.attrib) #attrib 属性 son=root.makeelement('tt',attrib={'kk','vv'}) root.append(son) #再date里面微末加上<tt kk=’vv‘></tt> tree.write('ou t.xml',short_empty_elements=False)
检查QQ是否在线
import hashlib,requests from xml.etree import ElementTree as ET req=requests.get('http://www.webxml.com.cn//webservices/qqOnlineWebService.asmx/qqCheckOnline?qqCode=QQ号') result=req.text note=ET.XML(result)# 解析XML格式内容,XML接受一个参数,字符串,格式化我特殊对象 if note.text=='Y': print('在线') else: print('离线')
获取列车时刻表:
1 import hashlib,requests 2 from xml.etree import ElementTree as ET 3 req=requests.get('http://www.webxml.com.cn/WebServices/TrainTimeWebService.asmx/getDetailInfoByTrainCode?TrainCode=G666&UserID=') 4 result=req.text 5 note=ET.XML(result)# 解析XML格式内容,XML接受一个参数,字符串,格式化我特殊对象 6 for root in note.iter('TrainDetailInfo'):

浙公网安备 33010602011771号