You do not know Python gadgets, make your work more efficient Python

python increasingly popular as a programming language, not only because of its simple language, there are many ready-made packages can be called directly.

python there are a lot of gadgets to make your python to work more efficiently.

You do not know Python gadgets, make your work more efficient Python

 

1, - quickly share -

HTTP server

SimpleHTTPServer python is built-in web server, using port 8000 and HTTP protocol to share.

Can quickly build a HTTP server and shared services on any platform (Window, Linux, MacOS), only you need to build a good python environment.

python2 version:

python -m SimpleHTTPServer

python3 version:

python -m http.server

FTP server

ftp sharing requires third-party components support, installation command:

the install pyftpdlib PIP 
Python port number P -m-pyftpdlib

Access method: ftp: // IP: port.

2, - decompress -

Here are five kinds of use python-extracting compressed file: .gz .tar .zip .rar

zip
import zipfile
# zipfile压缩
z = zipfile.ZipFile('x.zip', 'w', zipfile.ZIP_STORED) #打包,zipfile.ZIP_STORED是默认参数
# z = zipfile.ZipFile('ss.zip', 'w', zipfile.ZIP_DEFLATED) #压缩
z.write('x2')
z.write('x1')
z.close()
#zipfile解压
z = zipfile.ZipFile('x.zip', 'r')
z.extractall(path=r"C:\Users\Administrator\Desktop")
z.close()
tar
import tarfile
# 压缩
tar = tarfile.open('your.tar', 'w')
tar.add('/Users/wupeiqi/PycharmProjects/bbs2.log', arcname='bbs2.log')
tar.add('/Users/wupeiqi/PycharmProjects/cmdb.log', arcname='cmdb.log')
tar.close()
# 解压
tar = tarfile.open('your.tar', 'r')
tar.extractall() # 可设置解压地址
tar.close()

gz

gz generally only compress a file, all often work with other packaging tools. For example can be the first packaged with tar as X.tar, and then compressed into X.tar.gz

Unzip gz, in fact, is a single file which is read out, Python methods such as the following:

gzip Import 
Import os
DEF un_gz (file_name):
"" "ungz ZIP File" ""
f_name file_name.replace = (. "GZ", "")
# get the name of the file, remove
g_file = gzip.GzipFile (file_name)
# create gzip objects
open (f_name, "w +"). the write (g_file.read ())
#gzip object with the read () opens, write open files created in ().
g_file.close ()
# close gzip objects

rar

Since rar generally used for the next window, need additional Python package rarfile.

installation:

Python setup.py install

unzip:

import rarfile
import os
def un_rar(file_name):
"""unrar zip file"""
rar = rarfile.RarFile(file_name)
if os.path.isdir(file_name + "_files"):
pass
else:
os.mkdir(file_name + "_files")
os.chdir(file_name + "_files"):
rar.extractall()
rar.close()

3, - pip common operations -

pip is famous Python package management tool, essential in Python development.

installation

Online installation

pip install <包名> 或 pip install -r requirements.txt

Local installation:

pip install <directory> / <filename> or pip install --use-wheel --no-index --find-links = wheelhouse / <package name>

Find package

pip search <package name>

Delete Package

pip uninstall <包名> 或 pip uninstall -r requirements.txt

View package information

pip show <package name>

Check the package dependencies are complete

pip check <package name>

View the list of installed packages

pip list

Export all installed packages

pip freeze requirements.txt

4 - Conversion Json string -

json turn str

import json
str = '{"name": "zyl", "age": "two"}'
p = json.loads(str)
print(p)
print(type(p))

json turn str

Use json.dumps method, json object can be converted to a string.

s = {'name':'zyl','age':'22'}
s = json.dumps(s)

5, - python read excel -

step

· Install python library official Excel -> xlrd

· Excel file location and get read

· Read sheet

· Read the contents of the specified rows and cols

Examples

# - * - Coding: UTF-. 8 - * - 
Import to xlrd
from Import DATE datetime, datetime
DEF read_excel ():
# File Location
ExcelFile = xlrd.open_workbook (r'C: \ Users \ Administrator \ Desktop \ TestData.xlsx ')
# obtain the target sheet EXCEL file name
Print ExcelFile.sheet_names ()
# if a plurality of sheet, it is necessary to specify the read target sheet e.g. read Sheet2
# sheet2_name = ExcelFile.sheet_names () [. 1]
# [Get sheet 1. the contents of the index sheet on the sheet name]
# ExcelFile.sheet_by_index sheet = (. 1)
sheet ExcelFile.sheet_by_name = ( 'TestCase002')
# name of the print sheet, the number of rows, columns
print sheet.name, sheet.nrows, sheet.ncols
# Get the entire row or entire column values
rows = sheet.row_values (2) # third line
cols = sheet.col_values (1) # the second column contents
Print cols, rows
# obtaining cell contents
print sheet.cell(1,0).value.encode('utf-8')
print sheet.cell_value(1,0).encode('utf-8')
print sheet.row(1)[0].value.encode('utf-8')
#打印单元格内容格式
print sheet.cell(1,0).ctype
if__name__ =='__main__':
read_excel()

6, - python Screenshot -

python achieve screenshot function, windows environment, need to use the PIL library.

installation:

pip install Pillow

Example:

from the PIL Import ImageGrab 
BBOX = (X1, Y1, X2, Y2)
# X1: Start screenshot of the x coordinate; x2: Start screenshot y coordinate; x3: End screenshot of the x coordinate; x4: End screenshot y coordinate
im = ImageGrab .grab (BBOX)
im.save ( 'as.png') # save the screenshot file path

7、- ipython -

Finally, it shows a powerful tool for python --IPython.

IPython support variable auto-completion, auto-indent, support bash shell commands, it built many useful features and functions;

It is a Python for Humans is an interactive shell, after using it you do not want to use the built-in Python shell.

Guess you like

Origin www.cnblogs.com/qingdeng123/p/11311796.html