码迷,mamicode.com
首页 > 其他好文 > 详细

day6

时间:2016-02-22 12:06:19      阅读:156      评论:0      收藏:0      [点我收藏+]

标签:

一、加密模块

1.hashlib

技术分享
>>> data=hashlib.md5()
>>> data.update(bhello)
>>> print data.hexdigest()
5d41402abc4b2a76b9719d911017c592
View Code
技术分享
>>> data=hashlib.sha512()
>>> data.update(123456)
>>> print data.hexdigest()
ba3253876aed6bc22d4a6ff53d8406c6ad864195ed144ab5c87621b6c233b548baeae6956df346ec8c17f5ea10f35ee3cbc514797ed7ddd3145464e2a0bab413
View Code

2.hmac

技术分享
>>> import hmac
>>> h=hmac.new(bhello)
>>> h.update(btest)
>>> print h.hexdigest()
0c7490a8663ddd7b947eeec4e2ce85f9
View Code

 二、执行系统命令

1、os.system   只能返回执行之后的状态码

>>> result=os.system(‘ver‘)

Microsoft Windows [版本 6.1.7601]
>>> result
0

 

三、持久化

1、json

2、pickle

3、shelve

 

shelve模块是一个简单的k,v将内存数据通过文件持久化的模块,可以持久化任何pickle可支持的python数据格式

技术分享
import shelve

def shelve_write():
    dict1={username:root,password:123,host:localhost,prot:3306}
    list1=[name,root,db,mysql]

    d=shelve.open(shelve_test.txt)
    d[D1]=dict1  #持久化字典
    d[L1]=list1  #持久化列表
    d.close()


def shelve_read():
    d=shelve.open(shelve_test.txt)
    print(d[D1]) #读取字典
    print(d[L1]) #读取列表
shelve_read()
View Code

四、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>
技术分享
import xml.etree.ElementTree as ET

tree=ET.parse(test.xml)
root=tree.getroot()
# print root.tag


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


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


# #修改year节点
for node  in root.iter(year):
    new_year=int(node.text)+20
    node.text=str(new_year)
    node.set(update,yes)
tree.write(xym.xml)



#删除
for country in root.findall(country):
   rank = int(country.find(rank).text)
   if rank > 50:
     root.remove(country)
tree.write(output.xml)



#创建xml文档
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("test11.xml", encoding="utf-8",xml_declaration=True)

ET.dump(new_xml) #打印生成的格式
View Code

 

day6

标签:

原文地址:http://www.cnblogs.com/xuyanmei/p/5206374.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!