1. Encryption
encrypt md5 rsa hashlib ( for python2 )
案例一:
import hashlib
# m = hashlib.md5()
# src = "123456"
# m.update(src)
# print(m.hexdigest())
m3 = hashlib.md5("123456".encode("utf-8"))
src = bytes("ling", encoding="utf-8")
m3.update(src)
print(m3.hexdigest())
执行结果
2. StringIO module & BytesIO module 1. StringIO
module
In the usual development process, sometimes we may not need to write it in the file, we can directly write it into the system memory through the StringIO module, if it is not needed, it can be cleared directly. StringIO is mainly used to write strings in memory and cache strings. Its interface is the same as the interface of file operation. Basically, all methods related to files can be used, but only unicode type writing
case 2 is accepted.
from io import StringIO, BytesIO
stringIO = StringIO()
stringIO.write("hello world")
stringIO.write("lalalalla, wo shi mai bao de xiao hang jia")
print(stringIO.getvalue())
stringIO.truncate(0)
print(stringIO.getvalue())
执行结果
1.2 Initialize a StringIO with a string.
Case three:
from io import StringIO
output = StringIO(u'HELLO \nWORLDL \nLING \nJING\n')
# print(output.read())
print('aaaaaaaaaaaaaaaaaaa')
while 1:
s = output.readline()
if s == '':
break
print(s.strip())
执行结果:
2.bytesIO模块
StringIO can only operate on str. If you want to operate binary data, you need to use BytesIO
3. json module
Json, the full name of JavaScript Object Notation, is a lightweight data interchange format. The most widely used application of Json is as a data format for communication between web server and client in AJAX. It is also commonly used in http requests now, so various learning of json is a natural thing. And let's talk about the two methods that we use the longest in our daily work.
dumps and loads with s is the string dump and load without s is to process files
loads convert json strings into python objects. Many things give us a json string. We must first convert them into python objects before we can use them.
Case 4.
import json
a = dict(hello="hello")
print(a)
print(a["hello"])
print(str(a))
print(json.dumps(a, ensure_ascii=False))
execution result
Case 5:
import codecs
import json
test = {"a": 1, "b": 2}
with codecs.open("1.txt", "w") as f:
json.dump(test, f)
with codecs.open("1.txt", "r") as f:
aa = json.load(f)
print(aa)
print(type(aa))
执行结果: