[Python] Identify .yaml/.ini/.xml files

1. [Python] Identify .yaml/.ini/.xml files:

Background: In many cases in normal coding, fixed configuration information is stored in files, for example: .yaml/.ini/.xml files. This article introduces how to parse various forms of files

1.1. Module installation:

pip install PyYAML
我这里安装的是5.4.1版本

insert image description here

1.2, .yaml file read:

import yaml
from settings import *
# 读取.yaml文件方法,
# 1)在/config/settings.py文件中配置yaml文件路径;
# 2)在/config/ReadYaml.py文件中配置读取方法;

def read_config_yaml():  # 读取config.yaml文件方法
    path = CONFIG_YAML_PATH
    with open(path, encoding='utf-8') as file:
        data = yaml.safe_load(file)
        return data

1.3, yaml file write:

def write_cookie_yaml(conf):
    content = conf
    path = COOKIE_YAML_PATH
    with open(path, 'w', encoding='utf-8') as file:
        yaml.dump(content, file, default_flow_style=False, encoding='utf-8', allow_unicode=True)

1.4, yaml file update operation:

# coding:utf-8
from ruamel import yaml
# pip install ruamel.yaml==0.16.12

def up_yml_vendor(name, cookie):  # 提取vendor端cookie并存储对应路径下
    with open(COOKIE_YAML_PATH, encoding="utf-8") as f:
        content = yaml.load(f, Loader=yaml.RoundTripLoader)
        # 修改yml文件中的参数
        content['test']['yinni']['vendor'][f'{
      
      name}']['Cookie'] = f"{
      
      cookie}"
    with open(COOKIE_YAML_PATH, 'w', encoding="utf-8") as nf:
        yaml.dump(content, nf, Dumper=yaml.RoundTripDumper)

insert image description here

1.5, .ini file analysis:

1.5.1. Connection specified .ini file:

import configparser
from Akulaku_project8_Request.config.settings import *


class operate_ini(object):
    def __init__(self, Path: str):
        self.conf = configparser.ConfigParser()
        self.path = DATE_PATH + Path
        self.conf.readfp(open(self.path, encoding='utf-8'))
        self.items = []
        self.values = []

1.5.2. The method of reading the .ini file:

    def read_ini1(self, section: str, item: str):
        # 第一种读取ini文件方式,通过read方法,读取指定section下的单个item值
        value = self.conf[f'{
      
      section}'][f'{
      
      item}']
        # print('第一种方法读取到的值:', value)
        return value

    def read_ini2(self, section: str, item: str):
        # 第二种读取ini文件方式,通过get方法
        value = self.conf.get(f'{
      
      section}', f'{
      
      item}')
        # print('第二种方法读取到的值:', value)
        return value

    def read_ini3(self, section: str):
        # 第三种读取ini文件方式,读取到一个section中的所有数据,返回一个列表
        value = self.conf.items(f'{
      
      section}')
        # print('第三种方法读取到的值:', value)
        # 第三种方法读取到的值: [('name', 'demaxiya'), ('sex', 'man'), ('nation', 'Han')]
        return value

1.5.3. Get all section methods:

    def query_sections(self):
        # 获取所有的sections,已列表形式返回
        section = self.conf.sections()
        return section

1.5.4. Obtain the items and values ​​list according to the execution section:

    def get_item_values(self, section: str):
        # 输入一个section值获取items,values按照对应关系返回列表
        data = self.conf.items(f'{
      
      section}')
        items = self.items
        values = self.values
        for i in data:
            items.append(i[0])
            values.append(i[1])
        return [items, values]

1.5.5, .ini file new processing:

    def add_section(self, section: str):
        # 添加单个section
        section_list = operate_ini().query_sections()
        try:
            if section not in section_list:
                self.conf.add_section(f'{
      
      section}')
                self.conf.write(open(self.path, 'w', encoding='utf-8'))
        except Exception as e:
            print(f"发生错误,错误原因为:{
      
      e}")

    def add_option(self, section: str, item: str, value: str):
        operate_ini().add_section(section=section)
        self.conf.set(f"{
      
      section}", f"{
      
      item}", f"{
      
      value}")
        self.conf.write(open(self.path, 'r+', encoding='utf-8'))

1.6, .xml file analysis

1.6.1, connection.xml file

import xml.etree.ElementTree as ET  # 导入模块,名字太长了,把这个模块名重命名为ET
from settings import *

class operate_xml(object):
    def __init__(self, Path: str):
        self.path = DATE_PATH + Path
        self.tree = ET.parse(self.path)
        # parse解析,用ET模块下的parse这个方法把xml文件解析开,解析开拿到一个tree,tree就是一个对象
        self.root = self.tree.getroot()
        self.list = []
        # 这个对象可以调用方法,getroot就是根的意思
        # self.domTree = parse(self.path)

1.6.2, .xml file single query


    def get_root(self):  # 获取根元素名称
        root = self.root
        print([root[0]])  # root这个对象有一个属性tag,tag的值就是根标签的名字

    def get_first_tree(self):  # 获取DOM树根目录下首层标签名称
        for n in self.root:
            list = self.list
            list.append(n.tag)
        print(list)  # 获取DOM树根目录下首层标签名称,将结果以列表形式返回

    def get_inner_tree(self):  # 获取DOM树根目录下内层元素名称
        for n in self.root:
            for i in n:
                list = self.list
                list.append(i.tag)
        print(list)  # 获取DOM树根目录下内层元素名称,将结果以列表形式返回

    def get_first_attrib(self):  # 获取DOM树根目录下首层标签属性值
        for n in self.root:
            list = self.list
            list.append(n.attrib)
        print(list)  # 获取DOM树根目录下首层标签属性值,结果以列表形式返回

1.6.3. Get inner label attributes & label text

    def get_inner_text(self):
        # 获得里层标签文本值
        for n in self.root:
            for i in n:
                list = self.list
                list.append(i.text)
        print(list)

    def get_text(self, id: str):
        # 根据一级标签属性值,遍历标签文本值
        for n in self.root:
            a = dict(n.attrib).values()
            if f'{
      
      id}' == list(a)[0]:
                for i in n:
                    rv = self.list
                    rv.append(i.text)
                return rv

1.6.4. Traverse the label text according to the first-level label attributes

    def get_tag_text(self, id: str, tag: str):
        # 根据一级标签属性值,遍历标签文本值
        for n in self.root:
            a = dict(n.attrib).values()
            if f'{
      
      id}' == list(a)[0]:
                for i in n:
                    if i.tag == tag:
                        rv = self.list
                        rv.append(i.text)
                return rv

1.6.5. According to the first-level label attributes, add label text

    def add_text(self, id: str, attrib: str, text: str):
        # 根据一级标签属性值,添加标签文本值
        for n in self.root:
            a = dict(n.attrib).values()
            if f'{
      
      id}' == list(a)[0]:
                sex = ET.SubElement(n, f"{
      
      attrib}")
                sex.text = f"{
      
      text}"
        self.tree.write(TEST_XML_PATH, encoding='utf-8', xml_declaration=True, method='xml')
        # self.domTree.writexml(TEST_XML_PATH, addindent=' ', encoding='utf-8')

1.6.6. Create a new .xml file:

    def new_xml(self):  # 新建一个xml文件
        new_xml = ET.Element("namelist")  # 创建了一个根节点
        # 相当于创建了<namelist></namelist>
        name = ET.SubElement(new_xml, "name", attrib={
    
    "enrolled": "yes"})
        # 创建一个子标签name,然后增加一个属性
        age = ET.SubElement(name, "age", attrib={
    
    "checked": "no"})
        sex = ET.SubElement(name, "sex")
        sex.text = "28"

        et = ET.ElementTree(new_xml)  # 生成文档对象
        et.write("dema.xml", encoding="utf8", xml_declaration=True)

Guess you like

Origin blog.csdn.net/weixin_52358204/article/details/125217579