Crawling, grabbing tickets, signing in, I have done all the routine things, and everything is done, but it is not interesting. It was not until I discovered this by accident that I really opened the door to a new world.
Using Python as a script is a relatively vulgar thing. After all, it is to use a few lines of code to solve some tedious and complicated problems, so that people can free up time to do other things.
As an insider, improving efficiency is a small problem. As long as the demand scenario is given, we can rationalize the logic a little bit, and a few lines of code can solve it. Occasionally, we can open a few scripts for outsiders to earn some pocket money. ,But this kind of simple fun really can’t satisfy us.
In desperation, I could only go to Github to complain. Unexpectedly, I found a lot of real guys. After a rough estimate, there must be thousands of scripts.
The things I dug out, of course, can’t be let go. I will come to Zhihu to share with you as soon as possible. The script covers many fields, includingalgorithms, video, audio, crawler, web development, file processing, Visualization, system… We will briefly pick a few later.
The author is committed to creating Python classic small examples and scripts, just to say goodbye to the boring, um, I want to go with me.
Updated 25 days ago, indicating that the timeliness of the content inside is guaranteed, and each item also corresponds to a degree of difficulty, which is suitable for Xiaobai to practice when getting started and improve his programming ability.
In fact, these cases have given source code, but some people always copy and paste, simply to solve the problem to solve the problem, do not know what is the structure and logic of the dozens of lines of code in it.
I have always emphasized to newcomers, don’t copy other people’s things mechanically. When you get a source code, first look at what problem the other party uses to solve the problem, which architecture they use, and which one they choose in terms of grammatical expression. One, after determining the author’s thinking, can you rebuild it with your own architecture to achieve the same function.
Instead of looking at the ocean and sighing, it is better to try it yourself, only by practicing can true knowledge come out.
Share a few small cases:
- Batch compress files
import zipfile # Import zipfile, which is a Python module for compression and decompression; import os import time def batch_zip(start_dir): start_dir = start_dir # The path of the folder to be compressed file_news = start_dir + '.zip' # The name of the compressed folder z = zipfile.ZipFile(file_news, 'w', zipfile.ZIP_DEFLATED) for dir_path, dir_names, file_names in os. walk(start_dir): # This sentence is very important, if you don't replace, start copying from the root directory f_path = dir_path. replace(start_dir, '') f_path = f_path and f_path + os.sep #Compress the current folder and all files it contains for filename in file_names: z.write(os.path.join(dir_path, filename), f_path + filename) z. close() return file_news batch_zip('./data/ziptest')
- Automatic mass mailing
import smtplib from email import (header) from email.mime import (text, application, multipart) import time def sender_mail(): smt_p = smtplib. SMTP() smt_p.connect(host='smtp.qq.com', port=25) sender, password = '[email protected]', "****************" smt_p. login(sender, password) receiver_addresses, count_num = [ '[email protected]', '[email protected]'], 1 for email_address in receiver_addresses: try: msg = multipart.MIMEMultipart() msg['From'] = "zhenguo" msg['To'] = email_address msg['subject'] = header.Header('This is the email subject notification', 'utf-8') msg.attach(text.MIMEText( 'This is a test email, please do not reply to this email~', 'plain', 'utf-8')) smt_p. sendmail(sender, email_address, msg. as_string()) time. sleep(10) print('%d sent to %s' % (count_num, email_address)) count_num = count_num + 1 except Exception as e: print('The %d email sent to %s is abnormal' % (count_num, email_address)) continue smt_p. quit() sender_mail()
Note: The sending mailbox is a qq mailbox, so the SMTP service must be enabled in the qq mailbox. When the setting is completed, an authorization code will be generated, and this authorization code will be assigned to the password variable in the text.
- Melt perspective data tips
The melt method fixes a column as one dimension and combines other columns as another dimension to melt a wide table into a long table:
zip_code factory warehouse retail 0 12345 100 200 1 1 56789 400 300 2 2 101112 500 400 3 3 131415 600 500 4
Fixed column zip_code
, combination of factory
, warehouse
, retail
three columns named as a dimension, according to this method After the two dimensions are combined, the data must become longer.
The pandas melt method is demonstrated as follows:
In [49]: df = df. melt(id_vars = "zip_code")
If the melt method is used, the parameter value_vars
is not assigned a value, and all remaining columns are value_vars by default, so the result is as follows:
zip_code variable value 0 12345 factory 100 1 56789 factory 400 2 101112 factory 500 3 131415 factory 600 4 12345 warehouse 200 5 56789 warehouse 300 6 101112 warehouse 400 7 131415 warehouse 500 8 12345 retail 1 9 56789 retail 2 10 101112 retail 3 11 131415 retail 4
If you only want to view factory and retail, just assign value_vars
to them:
In [62]: df_melt2 = df.melt(id_vars = "zip_code", value_vars=['factory', 'reta ...: il'])
result:
zip_code variable value 0 12345 factory 100 1 56789 factory 400 2 101112 factory 500 3 131415 factory 600 4 12345 retail 1 5 56789 retail 2 6 101112 retail 3 7 131415 retail 4
After melt pivots the data, the data must become longer because multiple columns are combined into one column.
There are still many methods, so I won’t list them one by one. Interested friends can go to Github to browse by themselves, and there may be unexpected surprises.
Beginners don’t need to be discouraged. The moment you print out Hello World, you have already taken the first step in programming, and every subsequent step will be smoother and smoother because of the inertia of your first step. This book, a set of tutorials, follow the case to code and understand, programming is not as difficult as you think.
About Python technology reserves
Learning Python well is good whether it is employment or sideline business to make money, but to learn Python, you still need to have a study plan. Finally, everyone will share a full set of Python learning materials to help those who want to learn Python!
For beginners with 0 basics:
If you are a zero-based novice, you can consider getting started with Python quickly.
On the one hand, the learning time is relatively short, and the learning content is more comprehensive and concentrated.
On the other hand, you can find a learning plan that suits you
Including: Python activation code + installation package, Python web development, Python crawler, Python data analysis, artificial intelligence, machine learning and other tutorials. Take you to learn Python systematically from zero foundation!
Introduction to zero-based Python learning resources
Python learning route summary
The technical points in all directions of Python are sorted out to form a summary of knowledge points in various fields. Its usefulness lies in that you can find corresponding learning resources according to the above knowledge points to ensure that you can learn more comprehensively. (Get the full set of tutorials at the end of the article)
Python essential development tools
Reminder: The space is limited, the folder has been packed, and the way to obtain it is at the end of the article
Python learning video 600 collection
Watching the zero-based learning video is the fastest and most effective way to learn. Following the teacher’s ideas in the video, it is still very easy to get started from the basics to the in-depth.
100 Python exercises
Check the learning results.