🍅 Author: Don't eat tomatoes
🍅 Introduction: CSDN blog expert 🏆, HDZ core group members 💪, Top 10 in the general list of station C ✌ Welcome to like, collect and comment
🍅 Exclusive benefits for fans (2 copies at the end of the article): resume template, PPT template, learning materials, interview question bank. Go to the end of the document to get it directly
catalogue
2. Operation database: MySQLdb
7. Data drawing analysis: Matplotlib
1. Send mail
There are several modules for accessing the Internet and handling network communication protocols. The simplest two are urllib.request for processing data received from urls and SMTP lib for sending e-mail:
import smtplib smtpObj = smtplib.SMTP( [host [, port [, local_hostname]]] )
Parameter Description:
- host: SMTP server host. You can specify the ip address or domain name of the host, such as runoob.com. This is an optional parameter.
- Port: if you provide the host parameter, you need to specify the port number used by the SMTP service. Generally, the SMTP port number is 25.
- local_hostname: if SMTP is on your local machine, you only need to specify the server address as localhost.
The Python SMTP object uses the sendmail method to send mail. The syntax is as follows:
SMTP.sendmail(from_addr, to_addrs, msg[, mail_options, rcpt_options])
Parameter Description:
- from_addr: mail sender address.
- to_addrs: string list, email address.
- msg: send message
Case:
#!/usr/bin/python # -*- coding: UTF-8 -*- import smtplib from email.mime.text import MIMEText from email.header import Header sender = 'from@runoob.com' # Official account: information technology think tank receivers = ['429240967@qq.com'] # To receive mail, you can set it as your QQ mailbox or other mailboxes # Three parameters: the first is the text content, the second is to set the text format, and the third is utf-8 to set the code message = MIMEText('Python Mail sending test...', 'plain', 'utf-8') message['From'] = Header("Tomato God", 'utf-8') # sender message['To'] = Header("test", 'utf-8') # recipient subject = 'Python SMTP Mail test' message['Subject'] = Header(subject, 'utf-8') try: smtpObj = smtplib.SMTP('localhost') smtpObj.sendmail(sender, receivers, message.as_string()) print "Mail sent successfully" except smtplib.SMTPException: print "Error: Unable to send message"
2. Operation database: MySQLdb
To install MySQL dB, please visit http://sourceforge.net/projects/mysql-python
Operate mysql to query data
import MySQLdb # Connect to database conn = MySQLdb.connect(host='localhost',user='root',passwd='xxxx',db='test1') # Get cursor object for operation cursor = conn.cursor() # More information: https://t.1yb.co/zHJo sql = "select * from world where someone like 'you' " # Execute sql cursor.execute(sql)
3. Web crawler: requests
Requests allows you to send natural HTTP/1.1 requests without manual labor. You don't need to add query strings to the URL manually, and you don't need to encode the POST data. Keep alive and HTTP connection pooling are 100% automated.
A simple crawler example:
#Official account: information technology think tank import requests heads = {} heads['User-Agent'] = 'Mozilla/5.0 ' \ '(Macintosh; U; Intel Mac OS X 10_6_8; en-us) AppleWebKit/534.50 ' \ '(KHTML, like Gecko) Version/5.1 Safari/534.50' response = requests.get('http://www.baidu.com',headers=headers)
4. Operation execl: pandas
# -*- coding: utf-8 -*- import xlrdimport xlwt from datetime import date,datetime def read_excel(): # Open file workbook = xlrd.open_workbook(r'F:\demo.xlsx') # Get all sheet s print workbook.sheet_names() # [u'sheet1', u'sheet2'] sheet2_name = workbook.sheet_names()[1] # Get sheet content according to sheet index or name sheet2 = workbook.sheet_by_index(1) # sheet index starts from 0 sheet2 = workbook.sheet_by_name('sheet2') # Name of sheet, number of rows and columns print sheet2.name,sheet2.nrows,sheet2.ncols # Gets the value of the entire row and column (array) rows = sheet2.row_values(3) # Get the fourth line cols = sheet2.col_values(2) # Get the contents of the third column print rows print cols # Get cell content print sheet2.cell(1,0).value.encode('utf-8') print sheet2.cell_value(1,0).encode('utf-8') print sheet2.row(1)[0].value.encode('utf-8') # Gets the data type of the cell content print sheet2.cell(1,0).ctype if __name__ == '__main__': read_excel()
5. Operating system interface
The os module provides many functions associated with the operating system.
>>> import os >>> os.getcwd() # Returns the current working directory 'C:\\Python34' >>> os.chdir('/server/accesslogs') # Modify the current working directory >>> os.system('mkdir today') # Execute the system command mkdir 0
It is recommended to use the "import os" style instead of "from os import *. This ensures that os.open(), which varies with the operating system, does not override the built-in function open().
os common commands
Serial number | method | function |
1 | os.access(path, mode) | Check permission mode |
2 | os.chdir(path) | Change current working directory |
3 | os.chflags(path, flags) | Set the marker of the path as a numeric marker. |
4 | os.chmod(path, mode) | change permission |
5 | os.chown(path, uid, gid) | Change file owner |
6 | os.chroot(path) | Change the root directory of the current process |
7 | os.close(fd) | Close file descriptor fd |
8 | os.closerange(fd_low, fd_high) | Close all file descriptors from fd_low (inclusive) to fd_high (not included), errors are ignored |
9 | os.dup(fd) | Copy file descriptor fd |
10 | os.dup2(fd, fd2) | Copy one file descriptor FD to another fd2 |
6. Data analysis: numpy
NumPy contains a large number of functions of various mathematical operations, including trigonometric functions, functions of arithmetic operations, complex number processing functions, etc.
NumPy provides a variety of sorting methods. These sorting functions implement different sorting algorithms. Each sorting algorithm is characterized by execution speed, worst-case performance, required workspace and algorithm stability. The following table shows a comparison of the three sorting algorithms.
trigonometric function
NumPy provides standard trigonometric functions: sin(), cos(), tan().
import numpy as np a = np.array([0,30,45,60,90]) print ('Sinusoidal values at different angles:') # Convert to radians by multiplying pi/180 print (np.sin(a*np.pi/180)) print ('\n') print ('Cosine value of the angle in the array:') print (np.cos(a*np.pi/180)) print ('\n') print ('Tangent value of angle in array:') print (np.tan(a*np.pi/180))
7. Data drawing analysis: Matplotlib
import numpy as np from matplotlib import pyplot as plt x = np.arange(1,11) y = 2 * x + 5 plt.title("Matplotlib demo") plt.xlabel("x axis caption") plt.ylabel("y axis caption") plt.plot(x,y) plt.show()
In the above example, the NP. Orange() function creates a value on the x-axis. The corresponding value on the y-axis is stored in another array object y. These values are plotted using the plot() function of the pyplot sub module of the matplotlib package.
The graph is displayed by the show() function.
8. String regular matching
The re module provides regular expression tools for advanced string processing. It can be said that regular expressions are necessary for crawlers. For complex matching and processing, regular expressions provide concise and optimized solutions: if only simple functions are required, string methods should be considered first, because they are very simple and easy to read and debug:
>>> 'tea for too'.replace('too', 'two') 'tea for two'
re.match function
re.match attempts to match a pattern from the start of the string. If the start is not matched successfully, match() returns none.
Function syntax:
re.match(pattern, string, flags=0)
Function parameter description:
parameter | describe |
---|---|
pattern | Matching regular expressions |
string | String to match. |
flags | Flag bit, used to control the matching method of regular expressions, such as case sensitive, multi line matching, etc. |
Matching succeeded. The re.match method returns a matching object, otherwise it returns None.
We can use the group(num) or groups() matching object function to get the matching expression.
Matching object method | describe |
---|---|
group(num=0) | group() can enter multiple group numbers at a time, in which case it will return a tuple containing the values corresponding to those groups. |
groups() | Returns a tuple containing all group strings, from 1 to the contained group number. |
9. Game development: pygame
- Pygame is a set of cross platform Python modules for creating video games.
- It consists of a computer graphics and sound library designed for use with the Python programming language.
- Pygame was formally written by Pete Shinners to replace PySDL.
- Pygame is suitable for creating client applications that can be wrapped in separate executables.
import pygame pygame.init() screen = pygame.display.set_mode((400, 500)) done = False while not done: for event in pygame.event.get(): if event.type == pygame.QUIT: done = True pygame.display.flip()
10. Data compression
The following modules directly support common data packaging and compression formats: zlib, gzip, bz2, zipfile, and tarfile.
>>> import zlib >>> s = b'witch which has which witches wrist watch' >>> len(s) 41 >>> t = zlib.compress(s) >>> len(t) 37 >>> zlib.decompress(t) b'witch which has which witches wrist watch' >>> zlib.crc32(s) 226805979
[fan benefits: send 2 books]
[title]: Python 3.x basic tutorial
[how to get]: leave a comment in the comment area to like the collection. Draw 2 people from the comment area through the python random function. I will talk privately about the winners, provide address information, and publicize the express record in the next issue.
[content introduction]
The Python 3.x basic tutorial aims to help readers master the basic knowledge of Python language, how to use Python language to realize programming, understand its development skills, and be familiar with the development process and problem solutions through practical cases.
The book is divided into 13 chapters, which are roughly divided into four parts: Chapter 1 ~ 4 introduce Python introduction and environment construction, python foundation and object-oriented programming knowledge; Chapter 5 ~ 7 introduces the knowledge of reading and writing files, built-in battery module and system programming; Chapter 8 ~ 11 introduces network programming, sending and receiving e-mail, graphical user interface and Web development; Chapters 12 and 13 integrate the knowledge points of the book through the explanation of two comprehensive cases, so as to deepen the readers' understanding of the knowledge.
This book is designed for beginners and enthusiasts without any Python foundation. No matter whether you are engaged in computer related majors, have Python project experience, or want to switch to computer related majors, you can quickly master the basic knowledge and development skills of Python through this book.
🍅 Industry information: pay attention to get PPT template, resume template, industry classic book PDF.
🍅 Communication plus group: the boss points out the maze. Your problems are often encountered by some people. Ask for resources to shout in the group.
🍅 Interview question bank: it is jointly contributed by small partners in the technology group. The hot real interview questions of large factories are constantly updated.
🍅 Learning materials: including programming language, algorithm, big data ecosystem components (Mysql, Hive, Spark, Flink), data warehouse, front end, etc.
👇👇👇 More fan benefits 👇👇👇