hashlib module
Convert any length of data into a fixed-length data string (usually represented by a hexadecimal string) through a function.
Using hashlib in Python2:
import hashlib m = hashlib.md5() # m <md5 HASH object @ 0x0000000001E5C800> src = "ling" m.update(src) print(m.hexdigest())
# 24c10be286b009f797d53126790fcfd8
Using hashlib in Python3:
import hashlib m = hashlib.md5() # m = hashlib.md5("123".encode("utf-8")) # Add a random number # m <md5 HASH object @ 0x0000000001E5C800> src = bytes( " ling " ,encoding = " utf-8 " ) src1 = bytes("zhangsan",encoding="utf-8") m.update(src) m.update(src1) print(m.hexdigest())
If the amount of data is large, update() can be called multiple times in blocks.
StringIO module
Sometimes data reading and writing is not necessarily a file, but can also be read and written in memory. StringIO is to read and write str in memory.
from io import StringIO # StringIO can only store strings stringIO = StringIO () stringIO.write("hello,world\n") stringIO.write( " hello,python " ) print (stringIO.getvalue()) # hello,world # hello,python stringIO.truncate(0) #Empty all written content stringIO.flush() #Flush the internal buffer print ( stringIO.getvalue ()) #No output, the content has been emptied
StringIO can also be read like a file:
from io import StringIO stringIO = StringIO("hello\nworld") while True: s = stringIO.readline() if s == "": break print(s.strip())
BytesIO模块
StringIO can only operate on str. If you want to operate binary data, you need to use BytesIO.
from io import BytesIO bytesIO = BytesIO() bytesIO.write("中文".encode("utf-8")) print(bytesIO.getvalue()) # 读取内容
Note: What is written is not str, but utf-8 encoded bytes.
BytesIO can also read the contents in the same way as reading a file:
from io import BytesIO bytesIO = BytesIO(b'\xe4\xb8\xad\xe6\x96\x87') bytesIO.read() b ' \ xe4 \ xb8 \ limit \ xe6 \ x96 \ x87 '
Json module
json refers to JavaScript Object Notation, and json is a lightweight text data interchange format.
usage:
loads convert strings to python objects (such as dictionaries, lists, etc.)
dumps converts python objects to strings
load converts the file into a python object
dump writes python objects to a file
To operate on strings, convert strings into python objects:
import json test = '''[{"a":1, "aa":11, "aaa":111},{"b":2, "bb":22, "bbb":333}]''' print(type(test)) # <type 'str'> newTest = json.loads(test) # 把字符串转换成python对象 print(type(newTest)) # <type 'list'> print(newTest[0]["a"]) # 1
For python2 garbled problem, use json to solve:
import json a = dict(hello= " Hello " ) print (a) # {' hello ': '\xe4\xbd\xa0\xe5\xa5\xbd'} print (a[ " hello " ]) #Hello print ( json.dumps(a,ensure_ascii=False)) #Convert python objects to strings # {"hello": "Hello"}
To operate on files, files and python objects are converted to each other:
import json test = {"a":1, "b":2} with codecs.open("1.txt","w") as f: json.dump(test, f) #Write python object to file with codecs.open( " 1.txt " , " r " ) as f: aa = json.load(f) #Convert the file into a python object, aa is unicode type print (aa) # {u'a': 1, u'b': 2} print (type(aa)) # <type 'dict'>