python learning modules - Database (mysql) -excel operation - to write the log - message encryption -md5

Module # 
# standard module
# python comes
# third-party modules
# pymysql
# write your own python file
# import modules substantive
# Import my_model
# to import modules run again
# sequence import module
# 1, under the current directory look for
# 2, look for environment variables in python inside
# if __name__ == '__main __' : # role is to import another file inside the python time will not execute the code inside

# a python file is a module

# pip install xxx ( to install the module)
# manually install the module
# pip install whl absolute file directory
# .gz file decompression after installation download the corresponding directory into execution setup.py install Python

# PIP List lists all currently installed modules
# pip freeze> python .txt # module into the third-party modules to txt file
# pip install -r python module .txt # specified batch file to install the module

enumeration function
l = ['xiaoming','111','2222']
for bianhao,value in enumerate(l):
print('编号%s -- > %s'%(bianhao,value))


Database operations
pymysql Import 

#ip Port User password DB


# autocommit database connection settings automatically submitted, if set to True, additions and deletions to the operation performed when it is not necessary to perform commit () method submitted
con = host pymysql.connect (= '118.24.3.40 ',
the User =' ', password =' ',
db =' ', Port = 3306, charset =' utf8 ', True autocommit =)
# establish the cursor (similar to the warehouse manager)
# CUR = con.cursor ()
CUR = con.cursor (pymysql.cursors.DictCursor) # specify the return type of a dictionary
# sql statement executed, the results can not be obtained
sql = '' # search
# sql = ""
cur.execute (sql)
# con.commit () # modify, add, delete, must commit to submit or can not take effect automatically if the connection is set to submit to the database, you need to perform this method
# sql get the results
print (cur.fetchall ()) # get the return value of all
# print (cur.fetchone () ) Get a #
# print (cur.fetchmany (10)) # take the specified number of pieces
cur.close()
con.close()

Operating excel
# Python operation Excel 
# Import to xlrd

# excel_book = xlrd.open_workbook ( 'F.: \ PythonWorker \ Day5 \ Virgo list of participants .xlsx')
# = Sheet1 excel_book.sheet_by_index (0) # acquired based on the index sheet
# sheet2 = excel_book.sheet_by_name ( 'operating') # the name acquisition sheet

# print (sheet2.row_values (0)) # acquires data specified row
# print (sheet2.col_values (0)) # acquires data of the specified column
# print (sheet2.cell (0 , 0) .value) # Gets the contents of the cell
# print (sheet2.nrows) # Get the number of rows
# print (sheet2.ncols) # Get the number of columns


# written Excel
# Import xlwt
#
# new_book = xlwt.Workbook ()
# sheet = new_book.add_sheet ( 'list of art')
#
# STU = [
# [. 1, 'jiajiju', 'Beijing' '186 232 424', 'F'],
# [2 'Han Min', 'Beijing' , '186232424','Female'],
# [3, 'coke Li Fei', 'beijing', '186232424 ', ' F'],
#]
# excle method of writing. 1
# #. 1 row number Row =
# for ROW_DATA in STU:
# COL 0 =
# for in ROW_DATA col_data:
# sheet.write (row, COL, col_data)
# 1 = # + COL each write a column is incremented
# row + = 1 # row number is incremented for each write line 1

# writing two methods excel
# I in Range for (len (STU)):
# for J in Range (len (STU [I])):
# sheet.write (I, J, STU [I] [J])

# * three writing methods excel ****************************************************************
# for i, ROW_DATA in the enumerate (STU, 1):
# for J, col_date in the enumerate ( ROW_DATA):
# sheet.write (I, J, col_date)

# sheet.write (0,0, 'name')
# sheet.write (0,1, 'learning style')
#
# Sheet.write(1,0,'aaa')
Sheet.write # (1,1, 'BBB')

# new_book.save ( 'student1.xls')


# modify Excel
Import to xlrd
from xlutils Import Copy
# open an Excel
Book xlrd.open_workbook = ( 'student1.xls')
# copy
new_booke = copy.copy (Book)
sheet = new_booke.get_sheet (0) # Gets sheet page
title = [ 'number', 'name', 'address', 'phone', 'gender']
for index, T_DATA the enumerate in (title):
sheet.write (0, index, T_DATA)

new_booke.save ( 'student1.xls')
# Print (the dir (new_booke))
# excel modified


log operations
nnlog Import, Time 
DEF write_log (Content):
with Open ( 'text.log', '+ A', encoding = 'UTF-. 8') AS FW:
Data The time.strftime = ( '%% Y-M-% D% H:% M:% S ')
S ='% S, S content% '% (Data, content)

#level set the logging level set backCount reserve several long log when a file is provided to generate the SD
log nnlog.Logger = (' the test.log ', Level =' The warn ', backCount =. 5, When =' D ')
log.debug (' debugging ')
log.info (' normal print information ')
log.warning (' warning ')
log. error ( 'error')
# log.surprise ()


Mail operations

yagmail Import 

# username = '[email protected]'
username = '[email protected]'
# password = 'houyafan123'
password = 'ldh8035336'
SMTP = 'smtp.163.com'

mail = yagmail.SMTP (User username = , password = password, the SMTP Host =)

# If you send more than one person a copy to more than one person should write multiple attachments List
to = '[email protected]'
# to = [ 'a.qq.com', 'b.qq .com ']
CC =' [email protected] '

Subject =' 123! '
Content =' 345 '
mail.send (to = to, CC = CC, Subject = Subject, Contents Content =, = Attachments' accessory attachments absolute path using a plurality of list mode')



MD5 encrypted

hashlib Import 

# MD5 encrypted
password = '123456'
# = CHR password (password)
# encrypted data needs to encode () is converted to bytes Type
S = hashlib.md5 (password.encode ())
Print (s.hexdigest ())

representing a binary #rb open
f = open ( 'Virgo list of participants .xlsx', 'RB')
Content reached, f.read = ()
SHA = hashlib.sha256 (Content)
Print (sha.hexdigest ()) is encrypted after # string

# salt
DEF my_md5 (Content, = Salt 'FJKDldfk'):
S = STR (Content) + Salt
MD5 = hashlib.md5 (s.encode ())
return md5.hexgigest ()

Guess you like

Origin www.cnblogs.com/huaixiaohai/p/11093817.html