当前位置: 首页 > 科技观察

你不知道的Python小玩意有哪些

时间:2023-03-21 13:01:29 科技观察

Python作为一种编程语言越来越受欢迎,不仅是因为语言简单,还有很多现成的包可以直接调用。python中还有大量的小工具可以让你的python工作更有效率。1、快速分享HTTP服务器SimpleHTTPServer是python内置的web服务器,使用8000端口和HTTP协议进行分享。它可以在任何平台(Window、Linux、MacOS)上快速搭建一个HTTP服务和共享服务,只需要搭建一个python环境。python2版本:python-mSimpleHTTPServerpython3版本:python-mhttp.serverFTP服务器ftp共享需要第三方组件支持,安装命令:pipinstallpyftpdlibpython-mpyftpdlib-pport号访问方式:ftp://IP:port。2.解压下面介绍使用python解压五种压缩文件:.gz.tar.zip.rarzipimportzipfile#zipfilecompressionz=zipfile.ZipFile('x.zip','w',zipfile.ZIP_STORED)#包,压缩文件。ZIP_STORED是默认参数#z=zipfile.ZipFile('ss.zip','w',zipfile.ZIP_DEFLATED)#compressz.write('x2')z.write('x1')z.close()#zipfile解压缩z=zipfile.ZipFile('x.zip','r')z.extractall(path=r"C:UsersAdministratorDesktop")z.close()tarimporttarfile#compresstar=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一般只压缩一个file,all通常与其他打包工具一起使用。比如可以先用tar打包成X.tar,再压缩成X.tar.gz解压gz。其实就是读取里面的单个文件。Python方法如下:importgzipimportosdefun_gz(file_name):"""ungzzipfile"""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。安装:Pythonsetup.pyinstall解压缩:importrarfileimportosdefun_rar(file_name):"""unrarzipfile"""rar=rarfile.RarFile(file_name)ifos.path.isdir(file_name+"_files"):passelse:os.mkdir(file_name+"_files")os.chdir(file_name+"_files"):rar.extractall()rar.close()3.pip常用操作pip是Python著名的包管理工具,在Python开发中必不可少。安装在线安装pipinstall或pipinstall-rrequirements.txt本地安装:pipinstall/或pipinstall--use-wheel--no-index--find-links=wheelhouse/找到packagepipsearch删除packagepipuninstall或pipuninstall-rrequirements.txt查看包信息pipshow检查包依赖是否完整pipcheck查看已安装的包列表piplistexportallinstalledpackagespipfreezerequirements.txt4.字符串和Json转换json转strimportjsonstr='{"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读取指定行列内容示例#-*-coding:utf-8-*-importxlrdfromdatetimeimportdate,datetimedefread_excel():#FilelocationExcelFile=xlrd.open_workbook(r'C:UsersAdministratorDesktopTestData.xlsx')#GetthetargetEXCELFilesheetnameprintExcelFile.sheet_names()#如果有多个sheet,需要指定读取的目标sheet,比如读取sheet2#sheet2_name=ExcelFile.sheet_names()[1]#获取sheet内容[1.根据sheet索引2.根据Sheetname]#sheet=ExcelFile.sheet_by_index(1)sheet=ExcelFile.sheet_by_name('TestCase002')#打印sheet名称,行号,列号printsheet.name,sheet.nrows,sheet.ncols#获取整行或整列的值rows=sheet.row_values(2)#第三行内容cols=sheet.col_values(1)#第二列内容printcols,rows#获取单元格内容printsheet.cell(1,0).value.encode('utf-8')printsheet.cell_value(1,0).encode('utf-8')printsheet.row(1)[0].value.encode('utf-8')#print单元格内容格式打印表。cell(1,0).ctypeif__name__=='__main__':read_excel()6.pythonscreenshotPython实现了截图功能。在windows环境下,需要PIL库。安装:pipinstallPillow示例:fromPILimportImageGrabbbox=(x1,y1,x2,y2)#x1:开始截图的x坐标;x2:开始截图的y坐标;x3:结束截图的x坐标;x4:ycoordinateofendscreenshotim=ImageGrab.grab(bbox)im.save('as.png')#保存截图文件的路径7.ipython最后介绍了一个强大的python工具——IPython。IPython支持变量自动补全、自动缩进、bashshell命令,以及许多内置的有用函数和函数;它是一个供人类使用的Python交互式shell,用过之后你不想再使用内置的Pythonshell。