设为首页 加入收藏

TOP

第五周 day5 python学习笔记(三)
2017-11-13 14:56:08 】 浏览:306
Tags:第五 day5 python 学习 笔记
011</year> <gdppc>13600</gdppc> <neighbor name="Costa Rica" direction="W"/> <neighbor name="Colombia" direction="E"/> </country> </data> View Code
import xml.etree.ElementTree as ET# 导入包

tree=ET.parse("xmltest.xml")
root=tree.getroot()
print(root.tag)

#遍历xml文档
for child in root:
    print(child.tag,child.attrib)
    for i in child:
        print(i.tag,i.text)

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

# 修改和删除xml文档内容
for node in root.iter("year"):
    new_year=int(node.text)+1
    node.text=str(new_year)
    node.set("updated","yes")

tree.write("xmltest.xml")

#删除节点
for country in root.findall("country"):
    rank=int(country.find("rank").text)
    if rank>50:
        root.remove(country)
tree.write("xmltest.xml")
View Code

9.pyYAML模块

Python也可以很容易的处理ymal文档格式,只不过需要安装一个模块,参考文档:http://pyyaml.org/wiki/PyYAMLDocumentation

10.ConfigParser模块

用于生成和修改常见配置文档

常见的软件文档格式如下:

[DEFAULT]
ServerAliveInterval = 45
Compression = yes
CompressionLevel = 9
ForwardX11 = yes

[bitbucket.org]
User = hg

[topsecret.server.com]
Port = 50022
ForwardX11 = no
View Code

 

import configparser
#configparser生成模块
config=configparser.ConfigParser()

config["DEFAULT"] = {'ServerAliveInterval': '45',
                      'Compression': 'yes',
                     'CompressionLevel': '9'}

config['bitbucket.org'] = {}
config['bitbucket.org']['User'] = 'hg'
config['topsecret.server.com'] = {}
topsecret = config['topsecret.server.com']
topsecret['Host Port'] = '50022'     # mutates the parser
topsecret['ForwardX11'] = 'no'  # same here
config['DEFAULT']['ForwardX11'] = 'yes'
with open('example.ini', 'w') as configfile:
   config.write(configfile)
View Code
import configparser
# configparser读取模块
conf=configparser.ConfigParser()
conf.read("example.ini")

print(conf.defaults())
print(conf.sections())
View Code

11.hashlib模块

hashlib用于加密操作,主要提供SHA1, SHA224, SHA256, SHA384, SHA512 ,MD5 算法

import hashlib

m=hashlib.md5()
m.update(b"Hello world")
m.update(b"It's me this is for test")
print(m.digest())#以二进制格式输出加密后的结果
print(m.hexdigest())#以十六进制格式输出加密后的结果

n=hashlib.sha256()
n.update("Hello world,this is from Alice to Bob,今天天气不错哦".encode("utf-8"))
print(n.digest())
print(n.hexdigest())
View Code

12re模块--正则表达式

常用正则表达式符号

  1 '.'     默认匹配除\n之外的任意一个字符,若指定flag DOTALL,则匹配任意字符,包括换行
  2 '^'     匹配字符开头,若指定flags MULTILINE,这种也可以匹配上(r"^a","\nabc\neee",flags=re.MULTILINE)
  3 '$'     匹配字符结尾,或e.search("foo$","bfoo\nsdfsf",flags=re.MULTILINE).group()也可以
  4 '*'     匹配*号前的字符0次或多次,re.findall("ab*","cabb3abcbbac")  结果为['abb', 'ab', 'a']
  5 '+'     匹配前一个字符1次或多次,re.findall("ab+","ab+cd+abb+bba") 结果['ab', 'abb']
  6 '?'     匹配前一个字符1次或0次
  7 '{m}'   匹配前一个字符m次
  8 '{n,m}' 匹配前一个字符n到m次,re.findall("ab{1,3}","abb abc abbcbbb") 结果'abb', 'ab', 'abb']
  9 '|'     匹配|左或|右的字符,re.search("abc|ABC","ABCBabcCD").group() 结果'ABC'
 10 '(...)' 分组匹配,re.search("(abc){2}a(123|456)c",
首页 上一页 1 2 3 4 下一页 尾页 3/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇python中文分词,使用结巴分词对p.. 下一篇python示例1(基本知识巩固)

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目