欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python讀取測(cè)試數(shù)據(jù)的多種方式

 更新時(shí)間:2021年08月08日 10:55:28   作者:也就那樣吧_  
本文主要介紹了python讀取測(cè)試數(shù)據(jù)的多種方式,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

一、通過創(chuàng)建.ini或.conf文件讀取

1、創(chuàng)建一個(gè)config.ini或者.conf文件,這種方法就是ini文件的讀取,如下

[api]
url = www.taobao.com
method = get
[mysql]
db=hello
port=3306

2、使用python的configparser庫讀取,代碼如下

from configparser import ConfigParser
import os
conn=ConfigParser()
#獲取ini文件的路徑
file_path = os.path.join(os.path.abspath('.'),'config.ini')
conn.read(file_path)
url=conn.get('api','url')
method=conn.get('api','method')
port=conn.get('mysql','port')
print(url,method,port)

3、運(yùn)行得到如下的值,讀取成功

www.taobao.com get 3306

二、通過yaml文件讀取

1、首先創(chuàng)建一個(gè).yaml文件,這里我創(chuàng)建login_data.yaml,具體數(shù)據(jù)格式大家自行g(shù)oogle,這里我隨便編寫一些數(shù)據(jù)

-
  caseid: 1
  method: post
  title: 正常登錄
  url: api/v1/login
  data:
    username: test
    password: 123456
  headers:
    Content-Type: application/json
    User-Agent: Mozilla/5.0
  expected: 0

2、命令行通過pip install pyyaml安裝yaml包,直接上代碼

import yaml
def read_file(file_path):
    with open(file_path, 'r', encoding='utf-8') as f:
    res = yaml.load(f,Loader=yaml.FullLoader)
    return res
if __name__ == '__main__':
    d = read_file(r'..\test_data\login\login_data.yml')
    print(d)

3、運(yùn)行結(jié)果如下,是一個(gè)列表

[{'caseid': 1, 'method': 'post', 'title': '正常登錄', 'url': 'api/v1/login', 'data': {'username': 'test', 'password': 123456}, 'headers': {'Content-Type': 'application/json', 'User-Agent': 'Mozilla/5.0'}, 'expected': 0}]

三、通過excel讀取

1、創(chuàng)建一個(gè)excel文件,隨便寫點(diǎn)數(shù)據(jù)

2、通過pandas讀取,當(dāng)然還有其他的很多庫,本人覺得這個(gè)最方便,直接轉(zhuǎn)化成dict方便使用

import pandas
def excel_to_dic(filename):
    df = pandas.read_excel(filename)
    data_list = []
    for i in df.index.values:
        data = df.iloc[i, [j for j in range(len(df.keys()))]].to_dict()
        data_list.append(data)
    return data_list
if __name__ == '__main__':
    file = r'C:\aaa.xlsx'#之前創(chuàng)建的文件地址
    data = excel_to_dic(file)
    print(data)


3、運(yùn)行結(jié)果如下

[{'name': 'zhangsan', 'phone': 13112345678, 'age': 20}, {'name': 'lisi', 'phone': 13867543678, 'age': 30}, {'name': 'wangwu', 'phone': 15967845632, 'age': 40}]

可以看出結(jié)果是把excel表中的每一行數(shù)據(jù)轉(zhuǎn)換成了一個(gè)dict,更加方便以后使用?。。?br />

到此這篇關(guān)于python讀取測(cè)試數(shù)據(jù)的多種方式的文章就介紹到這了,更多相關(guān)python讀取測(cè)試數(shù)據(jù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Python實(shí)現(xiàn)的字典值比較功能示例

    Python實(shí)現(xiàn)的字典值比較功能示例

    這篇文章主要介紹了Python實(shí)現(xiàn)的字典值比較功能,可實(shí)現(xiàn)針對(duì)字典格式數(shù)據(jù)的判斷、比較功能,涉及Python字典格式數(shù)據(jù)的遍歷、判斷等相關(guān)操作技巧,需要的朋友可以參考下
    2018-01-01
  • Python人臉識(shí)別之微笑檢測(cè)

    Python人臉識(shí)別之微笑檢測(cè)

    Python可以從圖像或視頻中檢測(cè)和識(shí)別你的臉,人臉檢測(cè)與識(shí)別也是計(jì)算機(jī)視覺領(lǐng)域的研究熱點(diǎn)之一。本文主要為大家介紹通過Python實(shí)現(xiàn)人臉識(shí)別之微信檢測(cè),需要的同學(xué)可以參考一下
    2021-12-12
  • Python 靜態(tài)方法和類方法實(shí)例分析

    Python 靜態(tài)方法和類方法實(shí)例分析

    這篇文章主要介紹了Python 靜態(tài)方法和類方法,結(jié)合實(shí)例形式分析了Python類、實(shí)例、靜態(tài)方法等相關(guān)概念、原理與使用技巧,需要的朋友可以參考下
    2019-11-11
  • python實(shí)現(xiàn)word/excel/ppt批量轉(zhuǎn)pdf的示例代碼

    python實(shí)現(xiàn)word/excel/ppt批量轉(zhuǎn)pdf的示例代碼

    這篇文章主要為大家詳細(xì)介紹了如何利用python實(shí)現(xiàn)word、excel、ppt批量轉(zhuǎn)pdf文件,文中的示例代碼講解詳細(xì),有需要的小伙伴可以參考下
    2023-09-09
  • 最新評(píng)論