当前位置: 移动技术网 > IT编程>脚本编程>Python > 有哪些你不知道的Python小工具

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

2019年11月01日  | 移动技术网IT编程  | 我要评论

学seo,恋姐倾心漫画全集,风之刀分集剧情

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

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

1. 快速共享

http服务器

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

能够在任意平台(window,linux,macos)快速搭建一个http服务和共享服务,只需要搭建好python环境。

python2版本:

python -m simplehttpserver

python3版本:

python -m http.server

ftp服务器

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

pip install pyftpdlib
python -m pyftpdlib-p端口号

访问方式: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()

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一般仅仅压缩一个文件,全部常与其它打包工具一起工作。比方能够先用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对象

rar

由于rar通常为window下使用,须要额外的python包rarfile。
安装:

python setup.py install

解压缩:

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常用操作

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

安装

在线安装

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

本地安装:

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

查找包

pip search <包名>

删除包

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

查看包信息

pip show <包名>

检查包依赖是否完整

pip check <包名>

查看已安装包列表

pip list

导出所有已安装包

pip freeze requirements.txt

4. 字符串与json转换

json转str

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

json转str

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

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

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()

6. python 截图

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

安装:

pip install pillow

示例:

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')#保存截图文件的路径

7. ipython

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

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

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网