[Python] Use Python to read the contents of YAML and CSV files

1. YAML file

1. Introduction to YAML files

In Web UI automated testing, the configuration information used can be saved in a file in YAML format. The YAML language uses blank symbols to represent indexes, which is suitable for expressing configuration files.
The basic syntax rules of YAML files are as follows:

  • Case Sensitive.
  • Use indentation to indicate hierarchical relationships.
  • Tabs are not allowed for indentation, only spaces are allowed.
  • The amount of whitespace you enter is not important, as long as elements at the same level are left-aligned.
  • "#" indicates a comment.

YAML files support a variety of data types.

Notice:

  • The content of the YAML file is processed by yaml.load.
  • Add the default parameter Loader=yaml.FullLoader to prohibit the execution of arbitrary functions through the default loader FullLoader.

2. Python processes YAML files

First create a file named "my_yaml_1.yml" with the following content:

websites:
  url: "http://localhost:81/redmine/"
  ip: "127.0.0.1"
#冒号前没有空格,冒号后一个空格

Then write a script to read that file:

import yaml

with open('my_yaml_1.yml','r',encoding='utf8') as f:
    data = yaml.load(f,Loader=yaml.FullLoader)
    print(data)
    print(data['websites']['url'])
    print(data['websites']['ip'])

The result of the operation is as follows:
insert image description here

In Web UI automated testing, the configuration information used by the system can be saved in a specific file in a composite structure. Therefore, a function is encapsulated to read the information of the YAML file. The script is as follows:

import yaml

def parse_yml(file,section,key):
    with open(file,'r',encoding='utf8') as f:
        data = yaml.load(f,Loader=yaml.FullLoader)
        return data[section][key]

if __name__ == '__main__':
    value = parse_yml('my_yaml_1.yml','websites','url')
    print(value)

The result of the operation is as follows:
insert image description here

2. CSV file

1. Introduction to CSV files

In automated testing, relatively complex test data can be placed in tables, such as excel or CSV files.
CSV files can be opened with WordPad and are comma-separated data files.
The steps to read the file with the help of code are as follows:

  • Import the CSV module.
  • Use csv.reader to process data.

2. Python processes YAML files

First create a file named "my_yaml_1.yml" with the following content:

name,password,status
admin,error,0
admin,root,1

Then write a script to read that file:

import csv

with open('my_csv.csv','r',encoding='utf8') as f:
    data = csv.reader(f)
    print(data)
    for i in data:
        print(i)

The running results are as follows:
insert image description here
In automated testing, the data in the CSV file needs to be returned as a nested list, so that the data can be used as test parameters. The sample code is as follows:

import csv

def parse_csv(file):
    mylist = []
    with open(file,'r',encoding='utf8') as f:
        data = csv.reader(f)
        for i in data:
            mylist.append(i)
        del mylist[0] #删掉标题行的数据
        return mylist

if __name__ == '__main__':
    data = parse_csv('my_csv.csv')
    print(data)

The result of the operation is as follows:
insert image description here

Guess you like

Origin blog.csdn.net/jylsrnzb/article/details/131463943