Day5

1.file

a.打开文件方式(读写方式)

读文件
打开一个文件用open()方法(open()返回一个文件对象,它是可迭代的):

>>> f = open('test.txt', 'r')

r表示是文本文件,rb是二进制文件。(这个mode参数默认值就是r)
如果文件不存在,open()函数就会抛出一个IOError的错误,并且给出错误码和详细的信息告诉你文件不存在:

>>> f=open('test.txt', 'r')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
FileNotFoundError: [Errno 2] No such file or directory: 'test.txt'

文件使用完毕后必须关闭,因为文件对象会占用操作系统的资源,并且操作系统同一时间能打开的文件数量也是有限的

>>> f.close()

由于文件读写时都有可能产生IOError,一旦出错,后面的f.close()就不会调用。所以,为了保证无论是否出错都能正确地关闭文件,我们可以使用try … finally来实现:

try:
    f = open('/path/to/file', 'r')
    print(f.read())
finally:
    if f:
        f.close()

写文件
写文件和读文件是一样的,唯一区别是调用open()函数时,传入标识符’w’或者’wb’表示写文本文件或写二进制文件:

>>> f = open('test.txt', 'w') # 若是'wb'就表示写二进制文件
>>> f.write('Hello, world!')
>>> f.close()

注意:'w’这个模式是酱紫:如果没有这个文件,就创建一个;如果有,那么就会先把原文件的内容清空再写入新的东西。所以若不想清空原来的内容而是直接在后面追加新的内容,就用’a’这个模式。
我们可以反复调用write()来写入文件,但是务必要调用f.close()来关闭文件。当我们写文件时,操作系统往往不会立刻把数据写入磁盘,而是放到内存缓存起来,空闲的时候再慢慢写入。只有调用close()方法时,操作系统才保证把没有写入的数据全部写入磁盘。忘记调用close()的后果是数据可能只写了一部分到磁盘,剩下的丢失了。所以,还是用with语句来得保险:

with open('test.txt', 'w') as f:
    f.write('Hello, world!')

python文件对象提供了两个“写”方法: write() 和 writelines()。
write()方法和read()、readline()方法对应,是将字符串写入到文件中。
writelines()方法和readlines()方法对应,也是针对列表的操作。它接收一个字符串列表作为参数,将他们写入到文件中,换行符不会自动的加入,因此,需要显式的加入换行符。

f1 = open('test1.txt', 'w')
f1.writelines(["1", "2", "3"])
#    此时test1.txt的内容为:123

f1 = open('test1.txt', 'w')
f1.writelines(["1\n", "2\n", "3\n"])
#    此时test1.txt的内容为:
#    1
#    2        
#    3

b.文件对象的操作方法

1 文件模式   操作
 2 r  以只读方式打开
 3 rU 或 Ua 以读方式打开, 同时提供通用换行符支持 (PEP 278)
 4 w  以写方式打开 (必要时清空)
 5 a  以追加模式打开 (从 EOF 开始, 必要时创建新文件)
 6 r+ 以读写模式打开
 7 w+ 以读写模式打开 (参见 w )
 8 a+ 以读写模式打开 (参见 a )
 9 rb 以二进制读模式打开
10 wb 以二进制写模式打开 (参见 w )
11 ab 以二进制追加模式打开 (参见 a )
12 rb+ 以二进制读写模式打开 (参见 r+ )
13 wb+ 以二进制读写模式打开 (参见 w+ )
14 ab+ 以二进制读写模式打开 (参见 a+ )

c.学习对excel及csv进行操作

1. 操作csv表格
以列表形式打开

import csv
f = open('csv_test.csv', 'r')       # 打开csv文件
csv_reader = csv.reader(f)          # 将打开的文件装换成csv可读的对象
for each in csv_reader:             # 打印,结果是个列表
    print(each)
f.close()

结果

['name', 'age', 'score']
['张三', '18', '100']
['王二', '17', '98']
['麻子', '20', '99']

以字典形式打开

import csv
f = open('csv_test.csv', 'r')       # 打开csv文件
csv_reader = csv.DictReader(f)          # 将打开的文件装换成csv可读的对象,注意是DictReader()
for each in csv_reader:
    print(each['name'])
f.close()

结果

张三
王二
麻子

2.将数据写进csv文件
以列表的方式

import csv
contents = [['id', 'name', 'hobby'],
            ['001', '老王', 'Java'],
            ['002','老胡','Python'],
            ['003','老李','PHP']]
with open('csv_test2.csv', 'a', newline='') as f:
    csv_writer = csv.writer(f)
    for each in contents:
        csv_writer.writerow(each)

结果
在这里插入图片描述
操作excel表格
1.从excel文件里读取数据

import xlrd

book = xlrd.open_workbook('excel_test1.xls') # 打开excel文件
sheet_1 = book.sheets()[0]  # 打开第一张表

num_rows = sheet_1.nrows    # 获取当前表的所有的行数
num_cols = sheet_1.ncols    # 获取当前表的所有的列数

# 遍历行, 会将每一行以列表的形式输出
for row in range(num_rows):
    row_values = sheet_1.row_values(row)    # 使用的函数是row_values()
    print(row_values)

# 遍历列, 会将每一列以列表的形式输出
for col in range(num_cols):
    col_values = sheet_1.col_values(col)    # 使用的函数是col_values()
    print(col_values)

# 遍历单元格, 从上到下从左到右输出每一个单元格的内容
for row in range(num_rows):
    for col in range(num_cols):
        cell_value = sheet_1.cell_value(row, col)   # 使用的函数是cell_value()
        print(cell_value)

说明
主要函数,属性

book = xlrd.open_workbook('excel_test1.xls') # 打开excel文件
sheet_1 = book.sheets()[0] # 打开第一张表

num_rows = sheet_1.nrows # 获取当前表的所有的行数
num_cols = sheet_1.ncols # 获取当前表的所有的列数

row_values = sheet_1.row_values(row) # 使用的函数是row_values()

col_values = sheet_1.col_values(col) # 使用的函数是col_values()

cell_value = sheet_1.cell_value(row, col) # 使用的函数是cell_value()

2. 将数据写进excel文件中

import xlwt

# 创建Workbook对象
book = xlwt.Workbook(encoding='utf-8', style_compression=0)

'''
Workbook类初始化时有encoding和style_compression参数
encoding='utf-8',为输入中文
style_compression,表示是否压缩
'''

# 添加表格
sheet = book.add_sheet('sheet1', cell_overwrite_ok=True)

'''
sheet1为表格的名称
cell_overwrite_ok,表示是否可以覆盖单元格
'''

# 向表sheet1中添加数据
sheet.write(0, 0, 'id')  # 前两个参数为单元格坐标,这里表示第一行第一列(从0开始),后面为内容
sheet.write(0,1, 'name')
sheet.write(1,0, '1')
sheet.write(1,1, '老胡')


# 保存到指定的Excel文件中
book.save('excel_test2.xls')

说明
主要函数

book = xlwt.Workbook(encoding='utf-8', style_compression=0) # 创建Workbook对象

sheet = book.add_sheet('sheet1', cell_overwrite_ok=True) # 添加表格

sheet.write(0, 0, 'id') # 前两个参数为单元格坐标,这里表示第一行第一列(从0开始),后面为内容

book.save('excel_test2.xls') # 保存文件

2.os模块

在自动化测试中,经常需要查找操作文件,比如说查找配置文件(从而读取配置文件的信息),查找测试报告(从而发送测试报告邮件),经常要对大量文件和大量路径进行操作,这就依赖于os模块,所以今天整理下比较常用的几个方法。网上这方面资料也很多,每次整理,只是对自己所学的知识进行梳理,从而加深对某个模块的使用。
1.当前路径及路径下的文件
os.getcwd():查看当前所在路径。
os.listdir(path):列举目录下的所有文件。返回的是列表类型。

>>> import os
>>> os.getcwd()
'D:\\pythontest\\ostest'
>>> os.listdir(os.getcwd())
['hello.py', 'test.txt']

2.绝对路径

os.path.abspath(path):返回path的绝对路径。

>>> os.path.abspath('.')
'D:\\pythontest\\ostest'
>>> os.path.abspath('..')
'D:\\pythontest'

3.查看路径的文件夹部分和文件名部分
os.path.split(path):将路径分解为(文件夹,文件名),返回的是元组类型。可以看出,若路径字符串最后一个字符是,则只有文件夹部分有值;若路径字符串中均无,则只有文件名部分有值。若路径字符串有\,且不在最后,则文件夹和文件名均有值。且返回的文件夹的结果不包含.os.path.join(path1,path2,…):将path进行组合,若其中有绝对路径,则之前的path将被删除。

>>> os.path.split('D:\\pythontest\\ostest\\Hello.py')
('D:\\pythontest\\ostest', 'Hello.py')
>>> os.path.split('.')
('', '.')
>>> os.path.split('D:\\pythontest\\ostest\\')
('D:\\pythontest\\ostest', '')
>>> os.path.split('D:\\pythontest\\ostest')
('D:\\pythontest', 'ostest')
>>> os.path.join('D:\\pythontest', 'ostest')
'D:\\pythontest\\ostest'
>>> os.path.join('D:\\pythontest\\ostest', 'hello.py')
'D:\\pythontest\\ostest\\hello.py'
>>> os.path.join('D:\\pythontest\\b', 'D:\\pythontest\\a')
'D:\\pythontest\\a'

os.path.dirname(path):返回path中的文件夹部分,结果不包含’’

>>> os.path.dirname('D:\\pythontest\\ostest\\hello.py')
'D:\\pythontest\\ostest'
>>> os.path.dirname('.')
''
>>> os.path.dirname('D:\\pythontest\\ostest\\')
'D:\\pythontest\\ostest'
>>> os.path.dirname('D:\\pythontest\\ostest')
'D:\\pythontest'

3.datetime模块

一、datetime基本操作
1、获取当前datetime

1 time = datetime.datetime.now()
2 print time
3 #输出如下日期-时间(精确到微秒,小数点后6位)
4 
5 2019-03-07 20:19:34.794000

2、获取当天date

1 time = datetime.date.today()
2 print time
3 #输出结果如下:
4 
5 2019-03-07

3、获取明天/前N天
明天

1 time = datetime.date.today()+datetime.timedelta(days=1)
2 print time
3 #输出结果为:
4 
5 2019-03-08

3天前

1 time = datetime.date.today()-datetime.timedelta(days=3)
2 print time
3 #输出结果为:
4 
5 2019-03-04

二、时间的数据类型转换
1、datetime类型转换为string类型

1 time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
2 print time
3 #输出结果为:
4 
5 2019-03-07 20:41:23

2、datetime类型转换为date类型(记住这种写法,去掉date就是具体时间,加上date就是日期)

1 time = datetime.datetime.now().date()
2 print time
3 #输出结果为:
4 
5 2019-03-07

三、再加上timedalta可以进行时间(天)的计算
例子:

1 time = datetime.datetime.now().date()-datetime.timedelta(days = 1)  #这里如果不写days默认也是days
2 print time
3 #输出结果为:
4 
5 2019-03-06

4.类和对象

1.类
类:具有相同属性和方法的对象的集合;
对象:万物皆对象;
语法:

class 类名:
    属性
    方法

定义一个类:

class Preson:
     def eat(self):
          print("正在吃饭");
      def sleep(self):
          print("正在睡觉")

类的命名
首字母大写
以大小写分割单词
不使用下划线“_”
2.创建对象
属性写在类外的情况,就是通过对象.属性,对象.方法()的方法调用
对象的三要素:属性(对象是什么)、方法(对象能做什么)、事件(对象如何响应)
相互关系:类是对象的抽象,对象是类的实例,类是一种抽象事物的分类,对象是一种实例;

5.正则表达式

python正则表达式详解
  正则表达式是一个很强大的字符串处理工具,几乎任何关于字符串的操作都可以使用正则表达式来完成,作为一个爬虫工作者,每天和字符串打交道,正则表达式更是不可或缺的技能,正则表达式的在不同的语言中使用方式可能不一样,不过只要学会了任意一门语言的正则表达式用法,其他语言中大部分也只是换了个函数的名称而已,本质都是一样的。下面,我来介绍一下python中的正则表达式是怎么使用的。
  首先,python中的正则表达式大致分为以下几部分:
元字符
模式
函数
re 内置对象用法
分组用法
环视用法

6.re模块

基于Python的正则表达式, 使用re模块:

  1. match()方法, 从字符串头部开始匹配
import re

content = 'The 123456 is my one phone number.'
print(len(content)) #字符串长度
result = re.match(r'^The\s\d+\s\w*', content) #使用match匹配, 第一个参数为正则表达式, 第二个为要匹配的字符串
print(result)
print(result.group()) #输出匹配内容
print(result.span()) #输出匹配内容的位置索引

结果:

34
<_sre.SRE_Match object; span=(0, 13), match='The 123456 is'>
The 123456 is
(0, 13)
  1. 匹配目标
import re

content = 'The 123456 is my one phone number.'
print(len(content)) #字符串长度
result = re.match(r'^The\s(\d+)\sis', content) #使用match匹配, 第一个参数为正则表达式, 第二个为要匹配的字符串
print(result)
print(result.group()) #输出匹配内容
print(result.group(1)) #输出第一个被()包裹的内容
print(result.span()) #输出匹配内容的位置索引

结果:

34
<_sre.SRE_Match object; span=(0, 13), match='The 123456 is'>
The 123456 is
123456
(0, 13)

3.通用匹配

import re

content = 'The 123456 is my one phone number.'
result = re.match(r'^The.*number.$', content) #使用match匹配, 第一个参数为正则表达式, 第二个为要匹配的字符串
print(result)
print(result.group()) #输出匹配内容
print(result.span()) #输出匹配内容的位置索引

结果:

<_sre.SRE_Match object; span=(0, 34), match='The 123456 is my one phone number.'>
The 123456 is my one phone number.
(0, 34)

4.贪婪与非贪婪

import re

content = 'The 123456 is my one phone number.'
print('贪婪匹配:')
result = re.match(r'^The.*(\d+).*', content) #使用match匹配, 第一个参数为正则表达式, 第二个为要匹配的字符串
print(result.group()) #输出匹配内容
print('result = %s'%result.group(1)) #输出第一个被()包裹的内容
print('-'*20)
print('非贪婪匹配:')
result = re.match(r'^The.*?(\d+).*', content) 
print(result.group())
print('result = %s'%result.group(1))

结果:

贪婪匹配:
The 123456 is my one phone number.
result = 6
--------------------
非贪婪匹配:
The 123456 is my one phone number.
result = 123456

5.修饰符 re.S

import re

content = '''The 123456 is
one of my phone.
'''
result = re.match('^The.*?(\d+).*?phone.', content, re.S)
if result:
    print(result.group(1))
else:
    print('result = None')
result2 = re.match('^The.*?(\d+).*?phone.', content)
if result2:
    print(result2.group(1))
else:
    print('result2 = None')

结果:

123456
result2 = None

7.http请求

一、python自带库----urllib2
python自带库urllib2使用的比较多,简单使用如下:

import urllib2
response = urllib2.urlopen('http://localhost:8080/jenkins/api/json?pretty=true')
print response.read()

简单的get请求

import urllib2
import urllib
post_data = urllib.urlencode({})
response = urllib2.urlopen('http://localhost:8080/, post_data)
print response.read()
print response.getheaders()

二、python自带库–httplib
httplib是一个相对底层的http请求模块,urlib就是基于httplib封装的。简单使用如下:

import httplib
conn = httplib.HTTPConnection("www.python.org")
conn.request("GET", "/index.html")
r1 = conn.getresponse()
print r1.status, r1.reason
data1 = r1.read()
conn.request("GET", "/parrot.spam")
r2 = conn.getresponse()
data2 = r2.read()
conn.close()

简单的get请求
我们再来看post请求

import httplib, urllib
params = urllib.urlencode({'@number': 12524, '@type': 'issue', '@action': 'show'})
headers = {"Content-type": "application/x-www-form-urlencoded",  "Accept": "text/plain"}
conn = httplib.HTTPConnection("bugs.python.org")
conn.request("POST", "", params, headers)
response = conn.getresponse()
data = response.read()
print data
conn.close()

三、第三方库–requests
发请get请求超级简单:

print requests.get('http://localhost:8080).text

就一句话,再来看看post请求

payload = {'key1': 'value1', 'key2': 'value2'}
r = requests.post("http://httpbin.org/post", data=payload)
print r.text

如果要认证:

url = 'http://localhost:8080'
r = requests.post(url, data={}, auth=HTTPBasicAuth('admin', 'admin'))
print r.status_code
print r.headers
print r.reason
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值