Measuring open 5 - Python (module, database operation, operation Excel, encryption)

1⃣️ module

Module is divided into: standard module (python own), third-party modules (such as pymysql), write their own python file (a python file is a module)

Import Module essence: the import module to run again

Find module order: 1 in the current directory; 2 environment variables in python;.  Print (sys.path) # python environment variables 

Several methods of loading modules:

  1) pip install ***

  2) .whl type of file to download to a path, and then pip install path /***.whl

  3) .tar.gz file types, codecs, and then proceeds to route the command window under decompression, performed python setup.py install

:( practical operation of the command window)

  pip list ➡️ lists which modules are currently installed

  pip freeze> third-party modules .txt ➡️ export module has been installed

  pip install -r to install third-party module .txt ➡️ document in accordance with

 

2⃣️ operational database

. 1  Import pymysql
 2  # IP Port User pwd DB 
. 3  
. 4 Conn = pymysql.connect (= Host ' *** ' , = User ' the root ' , password = ' 123456 ' , DB = ' *** ' , charset = ' UTF8 ' , = True the autocommit)     # establish a connection 
. 5 CUR = conn.cursor ()     # establish cursor 
. 6 SQL = ' SELECT * WHERE from User ID <10 ' 
. 7 cur.execute (SQL)     # just executed sql, returns no result 
8cur.fetchall = RES ()     # fetch all the data and returns the two-dimensional tuples 
. 9  # cur.fetchone returns a # 
10  # cur.fetchmany (10) Returns the specified number of # 
. 11  Print (RES)
 12 is  
13 is  cur.close ( )
 14 conn.Close ()     # need to close the cursor and links

Remarks about the database requires points:

  1. Searching Class statement can directly get the result with fetchall (), but change the type of statement, you need to commit, 'autocommiit = True' is a statement, in the face of the need to commit sql, automatically commit
  2. In addition to the return type of tuples, but also a dictionary, the specific operation:  CUR = conn.cursor (pymysql.cursors.DictCursor)  to return to the dictionary, according to a practical application needs to be set
  3. Connections and cursors will need to close

 

3⃣️ Operation Excel

Divided into three types, read (xlrd), write (xlwt) and modification (xlutils) 

read:

  

write:

  

modify:

  

 

4⃣️ encryption

md5 encryption: encryption irreversible; 2 bytes encryption types can

1 import hashlib
2 s = '123456'
3 res = hashlib.md5(s.encode())
4 print(res.hexdigest())

为了防止有些密码设置的过于简单,可以给密码加盐。这个概念类似于,把传过来的密码加上一串特定的复杂的字符串,然后加密,如下

1 import hashlib
2 s = '123456'
3 salt = 'skde*399)5545+'
4 str = s+salt
5 res = hashlib.md5(str.encode())
6 print(res.hexdigest())

Guess you like

Origin www.cnblogs.com/april-aaa/p/11083656.html