Python modules Hashlib, XLWT, Pymysql

Source: Internet
Author: User

First, XLWT

XLWT is a python third-party module that is primarily written to Excel. XLWT must be installed before use.

1. Installation

Enter pip install XLWT carriage return in the cmd window of the operating system to install it online.

When the installation is complete, you need to introduce import XLWT when writing Python usage

2. Write Excel

Book = XLWT. Workbook ()#Create a new ExcelSheet = Book.add_sheet ('Sheet1')#Add Sheet pageSheet.write (0,0,'name')#row, column, write contentSheet.write (0,1,'Age') sheet.write (0,2,'Sex') Book.save ('User.xls')#The end must be used. xls

For detailed usage, refer to

71172778

Https://www.cnblogs.com/linyfeng/p/7123423.html

Second, MySQL connection

MySQL is used to install the Pymysql module. The installation method also enters Pip install pymysql in the cmd window. Import Pymysql when used

MySQL connection is divided into several steps:

1, connect the database

2. Create cursors,

3. Execute SQL

4. Get Results

5. Close the cursor

6. Close the connection

Example: SELECT statement

ImportPymysqlcoon=Pymysql.connect (Host='localhost', user='Root', passwd='123456', Port=3306,db='Shujuku', charset='UTF8'    #Port must write int type,    #CharSet must write UTF8 here .) cur= Coon.cursor ()#Creating CursorsCur.execute ('select * from user;')#Execute SQL statementres = Cur.fetchall ()#gets all returned results, the return type is a nested tupleCur.close ()#Close CursorsCoon.close ()#Close Connection

The use of the SELECT statement is as above. However, the Update\insert\delete statement operation is somewhat different because they modify the database, so after the operation must commit

Example:

Coon =Pymysql.connect (Host='localhost', user='Root', passwd='123456', #host根据要连接数据库的实际ip填写. Port=3306,db='Shujuku', charset='UTF8'    #Port must write int type,    #CharSet must write UTF8 here .) cur= Coon.cursor ()#Creating CursorsCur.execute ('INSERT INTO User (ID,NAME,PASSWD) VALUE (1, "AA", "123456");')
#如果插入的数据值变量存储的话, you can write this: Cur.execute ("INSERT into user (ID,NAME,PASSWD ' values ('%s ', '%s ', '%s ');"% (ID,USERNAME,PASSWD))
#其中% (ID,USERNAME,PASSWD) must be written outside the double quotation marks. If it's written inside, it's a string.
)
Coon.commit () # Delete Update insert must be Coomitcur.close ()# Close cursor Coon.close ()# Close Connection

MySQL uses Select to find out the table data, the return format is a two-tier ancestor. If you want to re-operate the data, it is necessary to operate on this 2-layer tuple (' 1 ', ' Little Red ', ' 123456 '), (' 2 ', ' xiaoming ', ' 123456 ')

Example: Writing data that is read from a MySQL database to Excel

ImportPYMYSQL,XLWTdefSql_to_excel (res):#Print (res)book=XLWT. Workbook () sheet=book.add_sheet ('Stu') sheet.write (0, 0,'numbering')#row, column, write contentSheet.write (0, 1,'name') sheet.write (0,2,'Sex') J=1#Line     forTuple11inchRes:#get to inner tuplei = 0#column, starting from column No. 0 every time         forTinchTUPLE11:#gets the element from the inner tuple and loops through each element to the corresponding row, columnSheet.write (j,i,t) I=i+1J=j+1Book.save ('User.xls') Conn=pymysql.connect (host='localhost', user='Root', passwd='123456', Port=3306,db='Shujuku', charset='UTF8') cur=conn.cursor () Cur.execute ('select * from user;') Res=Cur.fetchall () sql_to_excel (res) cur.close () conn.close ()

Third, Hashlib

#加密, the common encryption method MD5, the resulting result is a fixed bit byte, usually represented by a 32-bit 16 binary string. MD5 encryption is not solvable.

The Hashlib is a built-in module that does not need to be installed and only needs to be imported when used Hashlib

Import='123456965'm.update (Passwd.encode ())    # cannot encrypt string directly, first convert string to bytes type Print (M.hexdigest ())

There are hashlib.sha512,hashlib.sha256 and so on, the string is longer and more secure, but relatively slow

Python modules Hashlib, XLWT, Pymysql

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.