Python entry to learn DAY19 (shelve, xml, configparser, hashlib) module
Getting Started with Python
DAY 19
Today's content: shelve module, xml module, configparser module, hashlib module
1.shelve (a dictionary object module automatic serialization)
What is the shelve module
is also a serialization
method
. 1. opne
2. Read and write
3. close
Features: The use method is relatively simple. Provide a file name to start reading
and writing. The reading and writing method is the same as that of the dictionary.
You can use it as a tape. The principle of dictionary with automatic serialization function
: pickle is used internally, so there is also a problem of poor cross-platform performance. Only you know how to get it.
Use: Can be considered when writing stand-alone programs
import shelve
# 序列化
sl = shelve.open("shelvetest.txt")
sl["date"] = "8-13"
sl["list1"] = ["123","456"]
sl.close()
# 反序列化
s2 = shelve.open("shelvetest.txt")
print(s2.get("list1"))
s2.close()
2. XML (Extensible Markup Language)
is a file format
for writing configuration files or data exchange.
Why do you need XML?
In order to continue data exchange between different platforms, in
order to make the exchanged data understandable to the other party, it is necessary to follow a certain syntax standard to write
XML syntax format:
Any start tag must have an end tag.
Another simplified syntax is available, where both start and end tags can be represented in one tag. This syntax is followed by a slash (/) immediately before the greater than sign
The tags must be nested in the proper order.
All attributes must have a value, and must be enclosed in double quotes around the value.
Note: There is only one tag in the outermost layer. This tag is called the root tag.
The first line should have a document declaration for high-speed computers
. For example: <?xml version="1.0" encoding="utf-8"?>
When When tags are nested, there will be a hierarchical relationship. If a tag is not wrapped by any other tags, it is the root tag (outermost)
Usage scenarios:
1. Configuration file
2. Regular data exchange such as getting a piece of news from the server
The difference with json: the
function is the same, it is a data format
xml was born before
json, the data of json is smaller than xml,
and json is currently the mainstream
XML processing in python:
the used module
ElmentTree represents the element tree of the entire file
Elment represents a node
attribute
1.text The text between the start tag and the end tag
2.attrib all attributes Dictionary type
3.tag tag name
method
get Get the value of an attribute
1. Parse XML to
find the tag
find and get it in the subtag The name matches the first
findall Get all tags whose names match in
subtags iter(tagname) Find in the full text [all tags that match return an iterator
2. Generate XML Parse a file
with ElmentTree
parse()
getroot() Get the root The label
write() is written to the file
3. Modify an attribute of the xml
set
remove a label
append a label
3.configparser (configuration file parsing module) is
used to parse read and write configuration files
Content: section option
What is a configuration file ? A file
used to provide some information needed by the program to run. Suffix ini cfg
Purpose: easy for users to modify
The content format of the configuration file
includes only two elements:
section section
option options
A file can have multiple sections
A section can have multiple options
Core function
1.sections get all
sections2.options get all
options3.get get a value pass in section option
Note: case insensitive
4. What is hashlib
hash?
It is an algorithm
for compressing and mapping data of any length to a fixed-length character (extracting features)
Features of hash:
1. If the input data is different, the obtained hash value may be the same
2. The input value cannot be obtained through the hash value
3. If the algorithm is the same, regardless of the length of the input data, the obtained hash value has the same length
Because of the above characteristics, the hash algorithm is often used for encryption and file verification.
There are many
commonly used encryption methods. MD5 is a hash algorithm . A
commonly used method to improve security is to add salt
, which is to make some changes to the data before encryption, such as reversing the order.
List:
import hashlib
md = hashlib.md5()
md.update("hello".encode("utf-8"))
print(md.hexdigest())
# 破解MD5可以尝试撞库 原理: 有一个数据库 里面存放了常见的明文和密文的对应关系 ,
# 所以我可以拿着一个密文取数据库中查找 有没有已经存在的明文 如果有撞库成功 能不能破解凭运气
# 假设我已经拿到了一个众多账号中的一个密码 我可以那这个密码 挨个测试你的所有账号 可能不能碰到运气
pwd_dic = {"123456":"e10adc3949ba59abbe56e057f20f883e","hello":"5d41402abc4b2a76b9719d911017c592"}
for i in pwd_dic:
if pwd_dic[i] == "5d41402abc4b2a76b9719d911017c592":
print(i)