1. 程式人生 > 實用技巧 >一.execl資料驅動

一.execl資料驅動

本文包括:
execl資料驅動、MySQL資料驅動、CSV資料驅動、爬取拉勾網例項、ddt測試驅動、對XML檔案的讀取、測試執行的log讀取…

Quick Start

一.execl資料驅動

1.xlutils簡要說明

導包:pip3 install xlutils

注意⚠️:xlutils在介些Execl檔案的時候,只識別.xls字尾的檔案,如果是.xlsx字尾的檔案被解析,.xlsx字尾的檔案會被破壞

2.介面自動化中對execl簡單的讀取

#!/user/bin/env python
#coding:utf-8
#Author:shenqiang
'''xlrd寫入檔案,同時清空原檔案,一般這種方法只用來讀'''
import xlrd
import os
'''拿到檔案的路徑'''
def base_path(filename = None):
    return os.path.join(os.path.dirname(__file__),filename)
'''讀取檔案內容'''
work = xlrd.open_workbook(base_path('execlTestFile.xls'))
'''以下標或者sheet名取對應的哪頁'''
sheet = work.sheet_by_index(0)
# sheet = work.sheet_by_name()
'''檢視檔案有多少行'''
print(sheet.nrows)
'''獲取單元格內容,第3行,第3列'''
print(sheet.cell_value(2,2))

  

本文包括:
execl資料驅動、MySQL資料驅動、CSV資料驅動、爬取拉勾網例項、ddt測試驅動、對XML檔案的讀取、測試執行的log讀取…

Quick Start

一.execl資料驅動

1.xlutils簡要說明

導包:pip3 install xlutils

注意⚠️:xlutils在介些Execl檔案的時候,只識別.xls字尾的檔案,如果是.xlsx字尾的檔案被解析,.xlsx字尾的檔案會被破壞

2.介面自動化中對execl簡單的讀取

1
#!/user/bin/env python
2
#coding:utf-8
3
#Author:shenqiang
4
5
'''xlrd寫入檔案,同時清空原檔案,一般這種方法只用來讀'''
6
import xlrd
7
import os
8
9
'''拿到檔案的路徑'''
10
def base_path(filename = None):
11
    return os.path.join(os.path.dirname(__file__),filename)
12
13
'''讀取檔案內容'''
14
work = xlrd.open_workbook(base_path('execlTestFile.xls'
))
15
16
'''以下標或者sheet名取對應的哪頁'''
17
sheet = work.sheet_by_index(0)
18
# sheet = work.sheet_by_name()
19
20
'''檢視檔案有多少行'''
21
print(sheet.nrows)
22
23
'''獲取單元格內容,第3行,第3列'''
24
print(sheet.cell_value(2,2))

3.改寫execl檔案的內容

1
#!/user/bin/env python
2
#coding:utf-8
3
#Author:shenqiang
4
5
import xlrd
6
import os
7
from xlutils.copy import copy
8
9
'''拿到檔案的路徑'''
10
def base_path(filename = None):
11
    return os.path.join(os.path.dirname(__file__),filename)
12
13
'''開啟檔案'''
14
work = xlrd.open_workbook(base_path('execlTestFile.xls'))
15
'''把檔案記憶體存在一個變數裡'''
16
file_content = copy(work)
17
'''拿到檔案需要改寫的sheet頁'''
18
file = file_content.get_sheet(0)
19
# print(file_content)
20
'''
21
定位檔案位置寫入內容
22
行和列以從0開始數下標
23
'''
24
file.write(2,2,'沈強')
25
'''儲存檔案,並且重新命名'''
26
file_content.save(base_path('execlTestFile.xls'))

4.configparser配置檔案的讀取(服務和資料庫連線)

配置檔名

config.ini

檔案內容:

1
[linux]
2
ip:10.0.13.26
3
port:22
4
username:root
5
password:W2ynE6b58wheeFho
6
7
[mysql]
8
ip:10.0.13.26
9
port:22
10
username:root
11
password:W2ynE6b58wheeFho

檔名

1
tryConfigparser.py

檔案內容

1
#!/usr/bin/env python
2
# -*- coding: utf-8 -*-
3
# @Author : shenqiang
4
5
import os
6
import configparser
7
8
'''拿到檔案的路徑'''
9
10
def base_path(filename=None):
11
    return os.path.join(os.path.dirname(__file__), filename)
12
13
'''函式的預設引數處理'''
14
def getConfigparser(Linux='linux'):
15
    '''例項化物件'''
16
    config = configparser.ConfigParser()
17
    '''讀取檔案內容'''
18
    config.read(base_path('config.ini'))
19
    ip = config.get(Linux, 'ip')
20
    port = config.get(Linux, 'port')
21
    username = config.get(Linux, 'username')
22
    password = config.get(Linux, 'password')
23
    return [ip, port, username, password]
24
25
print(getConfigparser(),type(getConfigparser()))
26
27
# '''遍歷檔案內容'''
28
# for i in range(len(getConfigparser())):
29
#     print(getConfigparser()[i])

5.mysql常用的一些操作指令

1
啟動MySQL服務
2
mysql.server start
3
4
停止MySQL服務
5
mysql.server stop
6
7
重啟MySQL服務
8
mysql.server restart
9
10
進入MySQL資料庫
11
mysql -u root -p
12
Password: 密文傳輸(shen6409175)
13
14
'''檢視資料庫'''
15
show databases;
16
'''選中資料庫'''
17
use students;
18
'''查看錶'''
19
show tables;
20
'''建立表'''
21
create table student(
22
   id int primary key,
23
   name varchar(50),
24
   age varchar(10),
25
   address varchar(100)
26
   );
27
'''查看錶結構'''
28
desc student;
29
'''查看錶設計'''
30
show create table student;