有哪些你不知道的Python小工具

开发 开发工具
python作为越来越流行的一种编程语言,不仅仅是因为它语言简单,有许多现成的包可以直接调用。

 Python作为越来越流行的一种编程语言,不仅仅是因为它语言简单,有许多现成的包可以直接调用。

python中还有大量的小工具,让你的python工作更有效率。

[[281043]]

1. 快速共享

HTTP服务器

SimpleHTTPServer是python内置的web服务器,使用8000端口和HTTP协议共享。

能够在任意平台(Window,Linux,MacOS)快速搭建一个HTTP服务和共享服务,只需要搭建好python环境。

python2版本:

python -m SimpleHTTPServer 
  • 1.

python3版本:

python -m http.server 
  • 1.

FTP服务器

ftp共享需要第三方组件支持,安装命令:

pip install pyftpdlib 
python -m pyftpdlib-p端口号 
  • 1.
  • 2.

访问方式:ftp://IP:端口。

2. 解压缩

这里介绍利用python解压五种压缩文件:.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:UsersAdministratorDesktop"
z.close() 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.

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() 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.

gz

gz一般仅仅压缩一个文件,全部常与其它打包工具一起工作。比方能够先用tar打包为X.tar,然后在压缩为X.tar.gz

解压gz,事实上就是读出当中的单一文件,Python方法例如以下:

 

 

import gzip 
import os 
def un_gz(file_name): 
"""ungz zip file""" 
    f_name = file_name.replace(".gz"""
#获取文件的名称,去掉 
g_file = gzip.GzipFile(file_name) 
#创建gzip对象 
open(f_name, "w+").write(g_file.read()) 
#gzip对象用read()打开后,写入open()建立的文件里。 
g_file.close() 
#关闭gzip对象 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.

rar

由于rar通常为window下使用,须要额外的Python包rarfile。

安装:

Python setup.py install 
  • 1.

解压缩:

 

 

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() 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.

3.pip常用操作

pip 是 Python 著名的包管理工具,在 Python 开发中必不可少。

安装

在线安装

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

本地安装:

pip install <目录>/<文件名> 或 pip install --use-wheel --no-index --find-links=wheelhouse/ <包名> 
  • 1.

查找包

pip search <包名> 
  • 1.

删除包

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

查看包信息

pip show <包名> 
  • 1.

检查包依赖是否完整

pip check <包名> 
  • 1.

查看已安装包列表

pip list 
  • 1.

导出所有已安装包

pip freeze requirements.txt 
  • 1.

4. 字符串与Json转换

json转str

 

 

import json 
str = '{"name": "zyl", "age": "two"}' 
p = json.loads(str) 
print(p) 
print(type(p)) 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

json转str

使用json.dumps的方法,可以将json对象转化为字符串。

 

 

s = {'name':'zyl','age':'22'
s = json.dumps(s) 
  • 1.
  • 2.

5. python读取excel

步骤

  • 安装python官方Excel库–>xlrd
  • 获取Excel文件位置并读取
  • 读取sheet
  • 读取指定rows和cols内容

示例

 

 

# -*- coding: utf-8 -*- 
import xlrd 
from datetime import date,datetime 
def read_excel(): 
 
#文件位置 
 
ExcelFile=xlrd.open_workbook(r'C:UsersAdministratorDesktopTestData.xlsx'
 
#获取目标EXCEL文件sheet名 
 
print ExcelFile.sheet_names() 
 
#若有多个sheet,则需要指定读取目标sheet例如读取sheet2 
 
#sheet2_name=ExcelFile.sheet_names()[1] 
 
#获取sheet内容【1.根据sheet索引2.根据sheet名称】 
 
#sheet=ExcelFile.sheet_by_index(1) 
 
sheet=ExcelFile.sheet_by_name('TestCase002'
 
#打印sheet的名称,行数,列数 
 
print sheet.name,sheet.nrows,sheet.ncols 
 
#获取整行或者整列的值 
 
rows=sheet.row_values(2)#第三行内容 
 
cols=sheet.col_values(1)#第二列内容 
 
print cols,rows 
 
#获取单元格内容 
 
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() 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.

6. python 截图

python实现截图功能,windows环境下,需要用到PIL库。

安装:

pip install Pillow 
  • 1.

示例:

 

 

from PIL import ImageGrab 
bbox = (x1, y1, x2,y2 ) 
# x1: 开始截图的x坐标;x2:开始截图的y坐标;x3:结束截图的x坐标;x4:结束截图的y坐标 
im = ImageGrab.grab(bbox) 
im.save('as.png')#保存截图文件的路径 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

7. ipython

最后介绍的示一个强大的python工具——IPython 。

IPython 支持变量自动补全,自动缩进,支持 bash shell 命令,内置了许多实用功能和函数;

它是一个 for Humans 的 Python 交互式 shell,用了它之后你就不想再用自带的 Python shell 了。

 

责任编辑:华轩 来源: 博客园
相关推荐

2016-07-22 17:55:07

云计算

2020-06-12 09:20:33

前端Blob字符串

2020-07-28 08:26:34

WebSocket浏览器

2022-10-13 11:48:37

Web共享机制操作系统

2021-02-01 23:23:39

FiddlerCharlesWeb

2011-09-15 17:10:41

2009-12-10 09:37:43

2023-12-21 14:40:09

Python编程语言

2020-01-29 19:40:36

Python美好,一直在身边Line

2021-01-05 11:22:58

Python字符串代码

2010-08-23 09:56:09

Java性能监控

2010-10-19 15:31:44

Java

2020-02-21 14:55:02

Python代码字符串

2019-11-25 14:05:47

Python装饰器数据

2020-09-11 08:48:52

Python 3开发代码

2020-09-15 08:35:57

TypeScript JavaScript类型

2022-11-04 08:19:18

gRPC框架项目

2020-06-12 07:36:33

Redis

2021-10-17 13:10:56

函数TypeScript泛型

2015-06-19 13:54:49

点赞
收藏

51CTO技术栈公众号