random shutil shevle xml

Posted lakei

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了random shutil shevle xml相关的知识,希望对你有一定的参考价值。

random 模块

import random
 
print(random.random())#(0,1)----float    大于0且小于1之间的小数
 
print(random.randint(1,3))  #[1,3]    大于等于1且小于等于3之间的整数
 
print(random.randrange(1,3)) #[1,3)    大于等于1且小于3之间的整数
 
print(random.choice([1,23,[4,5]]))#1或者23或者[4,5]
 
print(random.sample([1,23,[4,5]],2))#列表元素任意2个组合
 
print(random.uniform(1,3))#大于1小于3的小数,如1.927109612082716 
 
 
item=[1,3,5,7,9]
random.shuffle(item) #打乱item的顺序,相当于"洗牌"
print(item)
import random
def make_code(n):
    res=‘‘
    for i in range(n):
        s1=chr(random.randint(65,90))
        s2=str(random.randint(0,9))
        res+=random.choice([s1,s2])
    return res

print(make_code(9))

生成随机验证码

shutil 模块 可以操作全新啊的处理文件模块

# 基于路径的文件复制:
shutil.copyfile(source_file, target_file)

# 基于流的文件复制:
with open(source_file, rb) as r, open(target_file, wb) as w:
    shutil.copyfileobj(r, w)
    
# 递归删除目标目录
shutil.rmtree(target_folder)

# 文件移动
shutil.remove(old_file, new_file)

# 文件夹压缩
shutil.make_archive(file_name, format, archive_path)

# 文件夹解压
shutil.unpack_archive(unpack_file, unpack_name, format)

shelve 模块

 shelve模块比pickle模块简单,只有一个open函数,返回类似字典的对象,可读可写;key必须为字符串,而值可以是python所支持的数据类型

# 将序列化文件操作dump与load进行封装
s_dic = shelve.open("target_file", writeback=True) # 注:writeback允许序列化的可变类型,可以直接修改值
# 序列化::存
s_dic[‘key1‘] = ‘value1‘
s_dic[‘key2‘] = ‘value2‘
# 反序列化:取
print(s_dic[‘key1‘])
# 文件这样的释放
s_dic.close()

import shelve

f=shelve.open(rsheve.txt)
# f[‘stu1_info‘]={‘name‘:‘egon‘,‘age‘:18,‘hobby‘:[‘piao‘,‘smoking‘,‘drinking‘]}
# f[‘stu2_info‘]={‘name‘:‘gangdan‘,‘age‘:53}
# f[‘school_info‘]={‘website‘:‘http://www.pypy.org‘,‘city‘:‘beijing‘}

print(f[stu1_info][hobby])
f.close()

标准输入输出从错误流

import sys
sys.stdout.write(msg)
sys.stderr.write(msg)
msg = sys.stdin.readline()

# print默认是对sys.stdout.write(‘msg‘) + sys.stdout.write(‘
‘)的封装
# 格式化结束符print:print(‘msg‘, end=‘‘)

xml xml文件操作模块

import xml.etree.ElementTree as ET
# 读文件
tree = ET.parse("xmltest.xml")
# 根节点
root_ele = tree.getroot()
# 遍历下一级
for ele in root_ele:
    print(ele)
    
# 全文搜索指定名的子标签
ele.iter("标签名")
# 非全文查找满足条件的第一个子标签
ele.find("标签名")
# 非全文查找满足条件的所有子标签
ele.findall("标签名")

# 标签名
ele.tag
# 标签内容
ele.text
# 标签属性
ele.attrib

# 修改
ele.tag = "新标签名"
ele.text = "新文本"
ele.set("属性名", "新属性值")

# 删除
sup_ele.remove(sub_ele)

# 添加
my_ele=ET.Element(myEle)
my_ele.text = new_ele 
my_ele.attrib = {name: my_ele}
root.append(my_ele)

# 重新写入硬盘
tree.write("xmltest.xml")

 

xml的格式如下,就是通过<>节点来区别数据结构的:

技术图片
<?xml version="1.0"?>
<data>
    <country name="Liechtenstein">
        <rank updated="yes">2</rank>
        <year>2008</year>
        <gdppc>141100</gdppc>
        <neighbor name="Austria" direction="E"/>
        <neighbor name="Switzerland" direction="W"/>
    </country>
    <country name="Singapore">
        <rank updated="yes">5</rank>
        <year>2011</year>
        <gdppc>59900</gdppc>
        <neighbor name="Malaysia" direction="N"/>
    </country>
    <country name="Panama">
        <rank updated="yes">69</rank>
        <year>2011</year>
        <gdppc>13600</gdppc>
        <neighbor name="Costa Rica" direction="W"/>
        <neighbor name="Colombia" direction="E"/>
    </country>
</data>

xml数据
View Code

xml协议在各个语言里的都 是支持的,在python中可以用以下模块操作xml:

# print(root.iter(‘year‘)) #全文搜索
# print(root.find(‘country‘)) #在root的子节点找,只找一个
# print(root.findall(‘country‘)) #在root的子节点找,找所有
技术图片
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,child.attrib[name])
    for i in child:
        print(i.tag,i.attrib,i.text)
 
#只遍历year 节点
for node in root.iter(year):
    print(node.tag,node.text)
#---------------------------------------

import xml.etree.ElementTree as ET
 
tree = ET.parse("xmltest.xml")
root = tree.getroot()
 
#修改
for node in root.iter(year):
    new_year=int(node.text)+1
    node.text=str(new_year)
    node.set(updated,yes)
    node.set(version,1.0)
tree.write(test.xml)
 
 
#删除node
for country in root.findall(country):
   rank = int(country.find(rank).text)
   if rank > 50:
     root.remove(country)
 
tree.write(output.xml)
View Code
#在country内添加(append)节点year2
import xml.etree.ElementTree as ET
tree = ET.parse("a.xml")
root=tree.getroot()
for country in root.findall(country):
    for year in country.findall(year):
        if int(year.text) > 2000:
            year2=ET.Element(year2)
            year2.text=新年
            year2.attrib={update:yes}
            country.append(year2) #往country节点下添加子节点

tree.write(a.xml.swap)

自己创建xml文档:

技术图片
import xml.etree.ElementTree as ET
 
 
new_xml = ET.Element("namelist")
name = ET.SubElement(new_xml,"name",attrib={"enrolled":"yes"})
age = ET.SubElement(name,"age",attrib={"checked":"no"})
sex = ET.SubElement(name,"sex")
sex.text = 33
name2 = ET.SubElement(new_xml,"name",attrib={"enrolled":"no"})
age = ET.SubElement(name2,"age")
age.text = 19
 
et = ET.ElementTree(new_xml) #生成文档对象
et.write("test.xml", encoding="utf-8",xml_declaration=True)
 
ET.dump(new_xml) #打印生成的格式
View Code

 

以上是关于random shutil shevle xml的主要内容,如果未能解决你的问题,请参考以下文章

9.random_os_sys_shutil_shelve_xml_hashlib

python---基础知识回顾(模块sys,os,random,hashlib,re,json,xml,shutil,configparser,logging,datetime,time,集合,(代码

8.模块介绍 time &datetime模块 random os sys shutil json & picle shelve xml处理 yaml处理 configparser h

python学习道路(day6note)(time &datetime,random,shutil,shelve,xml处理,configparser,hashlib,logging模块,re

python shutil模块&random模块

15-常用模块