20/01/26 Python learning the basics (10)

Object persistence

  • Application scenario
    serialization and deserialization of: storing a result of the current data being evaluated, or progress to a certain physical state not connected; future in a particular context restore the data stored in the memory to go.
  • technology
  1. Flat file
  • Text File
    scores=[99,88,77,66]
    def write_score():
    	with open('datalist.txt','w',ecoding='utf8') as f:
    		f.write(string(scores))
    	print('文件写入完成...')
    def read_score:
    	with open('datalist.txt','r',ecoding='utf8') as f:
    		lst=eval(f.read())  #将传递的字符串转换为python表达式 
    	```
    	
    
  1. pickle
    encapsulating a series of methods interfaces, python any original type object can be serialized into a string file or local
  • Serialized as a string
    import pickle
    person={'name':'Tom','age':20}
    s=pickle.dumps(person) #将python类型序列化为字节字符串
    p=picle.loads(s) #从字符串反序列化对象(保留类型特征)
    
  • Serialized object to a file
    pickle.dump(person,open('pickle_db','wb'))   #.dump(obj,file)
    p=pickle.load(open('pickle_db','rb'))  #.load(file)
    
  1. shelve - wherein: a plurality of objects are stored in a file, similar to the form dictionary tables keys to access different object

    import shelve
    scores=[99,88,77]
    student={'name':'Tom','age':20}
    db=shelve.open('shelve_student')
    db['s']=student
    db['scores']=scores
    # 调用
    db['s']
    # 删除文件
    del db['scores']
    
  2. database

  3. SNAKE

String

  • Outline
    • Types of
      • str string
      • bytes bytes
      • byte array bytearray
    • Character Encoding Architecture
      • Character Set: assign a code to a character to represent in memory
      • Encoding Encoding: conversion character to the original byte form
      • Decoding Decoding: converting raw byte encoding name based on the character of the process
    • String is stored
      • Encoding only acts on a storage medium or intermediate conversion
      • Always stored in memory after the decoded text
  • Character Encoding
    • The ASCII American Standard Code for Information Interchange (number / letter / punctuation / line feed ...)
      stored in a byte 0-127
    • Latin-1
      is stored in a byte 128-255
    • UTF-8 guarantee compatibility while minimizing the storage volume
      of the variable bytes: 0-127 single-byte, double-byte storage 128-2047,> 2047 3-4byte
    • 16-UTF
      2 byte character storage (2byte as additional identification)
    • UTF-32
      4 byte
  • Built-in functions
    • ord ( 'A')> 65 acquires the character code point
    • chr (104)> h acquires the character corresponding to the current code point
    • str.encode ( 'coding') a specific character encoding
    • bytes.decode ( 'coding') decoded as the character encoding text characters
    • sys.getdefaultencoding () to view the system default encoding format
  • Type Conversion
    • bytes
      manual claiming B ''
      string code str.encode ()
      Constructor bytes (STR, 'coding') into encoded string (not exceed 256, by default only process one byte)
    • bytearray - list of similar properties may be employed
      ByteArray ( 'byte', 'coding')
      .decode () decodes a string
  • BOM processing byte order mark
    open ( 'data.txt', 'r | w', encoding = 'utf-8-to’)
Published 10 original articles · won praise 0 · Views 139

Guess you like

Origin blog.csdn.net/weixin_44602323/article/details/104089111