python作为越来越流行的一种编程语言,不仅仅是因为它语言简单,有许多现成的包可以直接调用。 python中还有大量的小工具,让你的python工作更有效率。 HTTP服务器 SimpleHTTPServer是python内置的web服务器,使用8000端口和HTTP协议共享。 能够在任意平台(Window,Linux,MacOS)快速搭建一个HTTP服务和共享服务,只需要搭建好python环境。 python2版本: python -m SimpleHTTPServer python3版本: FTP服务器 ftp共享需要第三方组件支持,安装命令: 访问方式:ftp://IP:端口。 这里介绍利用python解压五种压缩文件:.gz .tar .zip .rar 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()
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()
gzgz一般仅仅压缩一个文件,全部常与其它打包工具一起工作。比方能够先用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。 安装:
解压缩: 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() pip 是 Python 著名的包管理工具,在 Python 开发中必不可少。 安装
在线安装pip install <包名> 或 pip install -r requirements.txt
本地安装:pip install <目录>/<文件名> 或 pip install --use-wheel --no-index --find-links=wheelhouse/ <包名> 查找包
删除包 pip uninstall <包名> 或 pip uninstall -r requirements.txt 查看包信息 检查包依赖是否完整 查看已安装包列表 导出所有已安装包 pip freeze requirements.txt json转strimport 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) 步骤 ·安装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()
python实现截图功能,windows环境下,需要用到PIL库。 安装: 示例: 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')#保存截图文件的路径
最后介绍的示一个强大的python工具——IPython 。 IPython 支持变量自动补全,自动缩进,支持 bash shell 命令,内置了许多实用功能和函数; 它是一个 for Humans 的 Python 交互式 shell,用了它之后你就不想再用自带的 Python shell 了。
|