码迷,mamicode.com
首页 > 编程语言 > 详细

Python3.7应用实验(2)

时间:2020-06-19 12:25:41      阅读:56      评论:0      收藏:0      [点我收藏+]

标签:list   sel   nec   res   pen   val   文件操作   目录操作   add   

一:文件和目录操作

1:文件操作

>>> fp=open(‘d:/aaa.txt‘,‘w‘)

>>> fp.close()

 

2:目录操作

1)获取当前路径

>>> import os

>>> print (os.getcwd())

C:\Python37

 

(2)创建目录

>>> import os

>>> print (os.mkdir(‘d:/test‘))

 

(3)判断目录是否存在

>>> import os

>>> print (os.path.isdir(‘d:/test‘))

True

 

(4)判断是否为文件

>>> import os

>>> print (os.path.isfile(‘d:/test‘))

 

二:案例--编写脚本,采集web质量数据到excel

 

1:部署web平台(centos

注意关闭firewalldselinux

 

2:用pip安装模块

PycurlXlsxWriter

 

3:编写脚本

 

#__.__coding:utf-8 __.__

import os,sys

import pycurl

import xlsxwriter

 

URL="http://192.168.10.105"

c=pycurl.Curl()

c.setopt(pycurl.URL,URL)

c.setopt(pycurl.CONNECTTIMEOUT,10)

c.setopt(pycurl.TIMEOUT,10)

c.setopt(pycurl.NOPROGRESS,1)

c.setopt(pycurl.FORBID_REUSE,1)

c.setopt(pycurl.MAXREDIRS,1)

c.setopt(pycurl.DNS_CACHE_TIMEOUT,30)

 

indexfile=open(os.path.dirname(os.path.realpath(__file__))+"/content.txt","wb")

 

c.setopt(pycurl.WRITEHEADER,indexfile)

c.setopt(pycurl.WRITEDATA,indexfile)

c.perform()

 

NAMELOOKUP_TIME=c.getinfo(c.NAMELOOKUP_TIME)

CONNECT_TIME=c.getinfo(c.CONNECT_TIME)

TOTAL_TIME=c.getinfo(c.TOTAL_TIME)

HTTP_CODE=c.getinfo(c.HTTP_CODE)

SIZE_DOWNLOAD=c.getinfo(c.SIZE_DOWNLOAD)

HEADER_SIZE=c.getinfo(c.HEADER_SIZE)

SPEED_DOWNLOAD=c.getinfo(c.SPEED_DOWNLOAD)

 

print (u"HTTP状态码:%s" %(HTTP_CODE))

print (u"DNS解析时间:%.2f ms" %(NAMELOOKUP_TIME*1000))

print (u"建立连接时间:%.2f ms"%(CONNECT_TIME*1000))

print (u"传输总时间:%.2f ms" %(TOTAL_TIME*1000))

print (u"下载数据包大小:%d bytes/s" %(SIZE_DOWNLOAD))

print (u"HTTP头部大小:%d bytes/s" %(HEADER_SIZE))

print (u"平均下载速度:%d bytes/s" %(SPEED_DOWNLOAD))

indexfile.close()

c.close()

 

f=open(‘chart.txt‘,‘a‘)

f.write(str(HTTP_CODE)+‘,‘+str(NAMELOOKUP_TIME*1000)+‘,‘+str(CONNECT_TIME*1000)+‘,‘+str(TOTAL_TIME*1000)+‘,‘+str(SIZE_DOWNLOAD/1024)+‘,‘+str(HEADER_SIZE)+‘,‘+str(SPEED_DOWNLOAD/1024)+‘\n‘)

f.close()

 

workbook=xlsxwriter.Workbook(‘chart.xlsx‘)

worksheet=workbook.add_worksheet()

chart=workbook.add_chart({‘type‘:‘column‘})

 

title=[URL,u‘HTTP状态码‘,u‘DNS解析时间‘,u‘建立连接时间‘,u‘传输结束时间‘,u‘下载数据包时间‘,u‘HTTP头部大小‘,u‘平均下载速度‘]

 

format=workbook.add_format()

format.set_border(1)

 

format_title=workbook.add_format()

format_title.set_border(1)

 

format_title.set_bg_color(‘#00ff00‘)

format_title.set_align(‘center‘)

format_title.set_bold()

 

worksheet.write_row(0,0,title,format_title)

 

f=open(‘chart.txt‘,‘r‘)

line=1

for i in f:

    head=[line]

    lineList=i.split(‘,‘)

    lineList=list(map(lambda i2:int(float(i2.replace("\n",‘‘))),lineList))

    lineList=head+lineList

    worksheet.write_row(line,0,lineList,format)

    line+=1

 

average=[u‘平均值‘,‘=AVERAGE(B2:B)‘+str((line-1))+‘)‘,‘=AVERAGE(C2:C)‘+str((line-1))+‘)‘,‘=AVERAGE(D2:D)‘+str((line-1))+‘)‘,‘=AVERAGE(E2:E)‘+str((line-1))+‘)‘,‘=AVERAGE(F2:F)‘+str((line-1))+‘)‘,‘=AVERAGE(G2:G)‘+str((line-1))+‘)‘,‘=AVERAGE(H2:H)‘+str((line-1))+‘)‘]

worksheet.write_row(line,0,average,format)

f.close()

 

def chart_series(cur_row,line):

    chart.add_series({

        ‘categories‘: ‘=Sheet1!$B$1:$H$1‘,

        ‘values‘: ‘=Sheet1!$B$‘+cur_row+‘:$H$‘+cur_row,

        ‘line‘: {‘color‘:‘black‘},

        ‘name‘: ‘=Sheet1!$A‘+cur_row,

        })

 

for row in range(2,line+1):

    chart_series(str(row),line)

chart.set_size({‘width‘:876,‘height‘:287})

worksheet.insert_chart(line+2,0,chart)

workbook.close()

 

Python3.7应用实验(2)

标签:list   sel   nec   res   pen   val   文件操作   目录操作   add   

原文地址:https://www.cnblogs.com/ccshi/p/13162129.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!