Day10 Python Study Notes & Key Points of Attention

 Part 1: Python study notes

====================

12. object persistence

12.1. Flat file

12.1.1. Text File

  • Text files are stored persistent objects
  • Convert the string you pass over to Python expressions, so that you can run the same statement when Python
  • Examples
 1 #文本文件存储持久化对象
 2 
 3 scores = [88,99,77,55]
 4 
 5 def write_scores():
 6     with open('data_list.txt','w',encoding='utf8') as f:
 7         f.write(str(scores))
 8         print('File writing completed......')
 9 
10 def read_scores():
11     with open('data_list.txt','r' , Encoding = ' UTF8 ' ) AS F:
 12 is          LST = the eval (reached, f.read ())     # your pass over the string into a Python expression, so that you can run the same statement as Python 
13 is  
14          LST [0] 99 =
 15          Print (LST)
 16  
. 17  IF  the __name__ == ' __main__ ' :
 18 is      # write_scores () # First RUN to the this data_list.txt The Create File 
. 19      read_scores ()

12.2.pickle

12.2.1.pickle module

  • Serialized bit string
    • .dumps (obj): the target sequence into a string
    • .loads (s): The hair string conversion target sequence
    • Examples
1 import pickle
2 person = {'name':'Tom','age':20}
3 s = pickle.dumps(person)
4 s

Run Results: b '\ x80 \ x03} q \ x00 (X \ x04 \ x00 \ x00 \ x00nameq \ x01X \ x03 \ x00 \ x00 \ x00Tomq \ x02X \ x03 \ x00 \ x00 \ x00ageq \ x03K \ x14u.'

1 p = pickle.loads(s)
2 p 

Run Results: { 'name': 'Tom', 'age': 20}

  • Serialized object to a file
    • .dump(obj,file)
    • .load(file)
    • Examples
1 person = {'name':'Tom','age':20}
2 pickle.dump(person,open('pickle.db','wb'))
3 p = pickle.load(open('pickle.db','rb'))
4 p

Run Results: Out [14]: { 'name': 'Tom', 'age': 20}

12.3.shelve

12.3.1.shelve

The existence of multiple objects in a document

  • .open('dbfile')
  • .close()
  • db['key'] = obj
  • len (db)
  • del db['key']
  • Examples
. 1  # Shelve serialized custom class Example 
2  
. 3  Import The shelve
 . 4  
. 5  class Student:
 . 6      DEF  the __init__ (Self, name, Age):
 . 7          the self.name = name
 . 8          self.age = Age
 . 9  
10  
. 11      DEF  __str__ (Self):
 12 is          return the self.name
 13 is  
14  DEF write_shelve ():
 15          S = Student ( ' Tom ' , 20 is )
 16          DB = shelve.open ( 'shelve_student_db')
17         db['s'] = s
18         db.close()
19 
20 def read_shelve():
21     db = shelve.open('shelve_student_db')
22     st = db['s']
23     print(st)
24     print(st.name)
25     print(st.age)
26     db.close()
27 
28 
29 if __name__ == '__main__':
30     # write_shelve()    # First run this so that the shelve_student_db.* files can be created
31     read_shelve()

12.4. Database

12.5.ORM

13. String

13.1. Overview

13.1.1 Type

  • str string
  • bytes bytes
  • byte array bytearray

13.1.2. 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
  • Character encoding architecture diagram:

  

13.1.3. Strings are stored

  • Encoding only acts on the file storage media or intermediate conversion
  • Always stored in memory after the decoded text

13.2. Character Encoding

13.2.1.ASCII

  • Stored in a Byte 0-127
  • Stored in a Byte 128-255
  • Variable byte

13.2.2.latin-1

13.2.3.UTF-8

• 0-127 single-byte

• 128-2047 double-byte storage

•     > 2047 3-4 Byte

• Use 128-255 per Byte

13.2.4.UTF-16

  • 2 Byte store character (as identified plus 2 Byte)
  • 4 Byte

13.2.5.UTF-32

13.3. Built-in functions

13.3.1.ord () Gets the character code point

13.3.2.chr () Gets a character code corresponding to the point

13.3.3.str.encode ( 'coding') a specific character encoding

13.3.4.bytes.decode ( 'coding') character encoding and decoding text characters

13.3.5.encode and decode example

. 1 S1 = ' ABCD ' 
2 s1.encode ( ' the ASCII ' )
 . 3  
. 4 S2 = ' Youpin class ' 
. 5 s2.encode ( ' UTF-. 8 ' )
 . 6  
. 7 B1 = B ' \ XE4 \ XBC \ X98 \ xe5 \ X93 \ X81 \ xe8 \ XAF \ XBE \ xe5 \ XA0 \ X82 ' 
. 8  
. 9 b1.decode ( ' UTF-. 8 ' ) -> ' excellent product class ' 
10 b1.decode ( ' UTF-16 ' ) -> see garbled
  • Note: encode and decode, the default encoding is utf-8
1 import sys
2 sys.getdefaultencoding()
3 
4 --> utf-8

13.3.6. Formulate example when encoding type document processing

1 open('data.txt','w',encoding='utf-8').write('优品课堂')
2 open('data.txt','r',encoding='utf-8').read()

13.4 Type Conversion

13.4.1.bytes

  • Manual Statement b ''
  • String code str.encode ()
  • Constructor bytes ()
  • It does not support changing the in situ

13.4.2.bytearray

  • ByteArray ( 'character', 'coding')
  • .decode () decodes a string
  • Support situ change

13.5.BOM (Byte Order Mark) process

13.5.1.open('data.txt','w/r',encoding='utf-8-sig')

Guess you like

Origin www.cnblogs.com/hemin96/p/11423149.html