【Python文件读取学习】读取表格(csv、xls、xlsx)、读取xml、读取yaml类封装记录

一、读取表格(csv、xls、xlsx)

# _*_ coding:utf-8 _*-
import csv
import xlrd
from openpyxl import load_workbook


class RWFile:

    #  按行读取文件,根据文件后缀判断读取方式,最终返回行列表。其中,每行为一个列表。
    def get_content_lines(self, path, sheet_name=None):
        filetype = path.split(".")[-1].lower()
        if filetype == 'csv':
            return self.get_csv_lines(path)
        elif filetype in ['xls']:
            return self.get_excel_xls_lines(path, sheet_name)
        elif filetype in ['xlsx']:
            return self.get_excel_xlsx_lines(path, sheet_name)

    #  按行读取文件,根据文件后缀判断读取方式,最终返回行列表。其中,每行为一个字典。
    def get_content_dict(self, path, sheet_name=None):
        filetype = path.split(".")[-1].lower()
        if filetype == 'csv':
            return self.get_csv_dict(path)
        elif filetype in ['xls']:
            return self.get_excel_xls_dict(path, sheet_name)
        elif filetype in ['xlsx']:
            return self.get_excel_xlsx_dict(path, sheet_name)

    #  csv按行读取数据,每行数据为一个列表,最终以列表方式返回行列表。
    def get_csv_lines(self, path):
        filelines = []
        with open(path, newline='') as f:
            reader = csv.reader(f)
            try:
                for row in reader:
                    filelines.append(row)
            except csv.Error as e:
                print(e)
                filelines.clear()
        return filelines

    # csv按行读取数据,首行默认为字段名,从第二行开始读取,每行数据为一个字典,每个键值对为:字段名-值,最终每以列表方式返回行列表。
    def get_csv_dict(self, path):
        filelines = []
        with open(path, newline='') as f:
            reader = csv.DictReader(f)
            try:
                for row in reader:
                    filelines.append(row)
            except csv.Error as e:
                print(e)
                filelines.clear()
        return filelines

    # excel_xls按行读取数据,每行数据为一个列表,最终以列表方式返回行列表。
    def get_excel_xls_lines(self, path, sheet_name):
        filelines = []
        file = xlrd.open_workbook(path)
        sheet = file.sheet_by_name(sheet_name)
        for row in range(sheet.nrows):
            filelines.append(sheet.row_values(row))
        return filelines

    # excel_xls按行读取数据,首行默认为字段名,从第二行开始读取,每行数据为一个字典,每个键值对为:字段名-值,最终每以列表方式返回行列表。
    def get_excel_xls_dict(self, path, sheetName):
        filelines = []
        file = xlrd.open_workbook(path)
        sheet = file.sheet_by_name(sheetName)
        nrow = sheet.nrows
        ncol = sheet.ncols
        if nrow > 1:
            for row in range(1, nrow):
                row_dict = {}
                for col in range(0, ncol):
                    row_dict[sheet.cell(0, col).value] = sheet.cell(row, col).value
                filelines.append(row_dict)
        return filelines

    # excel_xlsx按行读取数据,每行数据为一个列表,最终以列表方式返回行列表。
    def get_excel_xlsx_lines(self, path, sheetName):
        filelines = []
        file = load_workbook(filename=path)
        sheet = file[sheetName]
        print(sheet.cell(row=1, column=2).value)
        for row in sheet.iter_rows(values_only=True):
            celllist = []
            for cell in row:
                celllist.append(cell)
            filelines.append(celllist)
        return filelines

    # excel_xlsx按行读取数据,首行默认为字段名,从第二行开始读取,每行数据为一个字典,每个键值对为:字段名-值,最终每以列表方式返回行列表。
    def get_excel_xlsx_dict(self, path, sheetName):
        filelines = []
        file = load_workbook(filename=path)
        sheet = file[sheetName]
        nrow = sheet.max_row
        ncol = sheet.max_column
        if nrow > 1:
            for row in range(2, nrow+1):
                row_dict = {}
                for col in range(1, ncol+1):
                    row_dict[sheet.cell(1, col).value] = sheet.cell(row, col).value
                filelines.append(row_dict)
        return filelines

二、读取xml

  # -*- coding: UTF-8 -*-

"""
读写xml
"""

import xml.etree.ElementTree as ET
import xml.dom.minidom as minidom


def read_xml(in_path):
    """读取并解析xml文件
       in_path: xml路径
       return: tree
    """
    tree = ET.parse(in_path)
    return tree


def xml_tree_to_dict(tree):
    """xml生成为dict,
    将tree中个节点添加到list中,将list转换为字典dict_init
    叠加生成多层字典dict_new
    """
    dict_new = {}
    for key, valu in enumerate(tree.getroot()):
        dict_init = {}
        list_init = []
        for item in valu:
            list_init.append([item.tag, item.text])
            for lists in list_init:
                dict_init[lists[0]] = lists[1]
        dict_new[key] = dict_init
    return dict_new


def dict_to_xml_tree(input_dict, root_tag, node_tag):
    """ 定义根节点root_tag,定义第二层节点node_tag
    第三层中将字典中键值对对应参数名和值
       return: xml的tree结构 """
    root_name = ET.Element(root_tag)
    for (k, v) in input_dict.items():
        node_name = ET.SubElement(root_name, node_tag)
        for (key, val) in sorted(v.items(), key=lambda e:e[0], reverse=True):
            key = ET.SubElement(node_name, key)
            key.text = val
    return root_name


def out_xml(root, path):
    """格式化root转换为xml文件"""
    rough_string = ET.tostring(root, 'utf-8')
    reared_content = minidom.parseString(rough_string)
    with open(path, 'w+') as fs:
        reared_content.writexml(fs, addindent=" ", newl="\n", encoding="utf-8")
    return True


def main():
    # root = read_xml('/Users/mac/MyCode/Repo/PycharmProjects/LoomoTestingTool/allure-results/environment.xml')
    # print(repr(root))
    # print(xml_tree_to_dict(root))

    d = {0: {'key': 'App.Platform', 'value': 'Android'}, 1: {'key': 'App.Version', 'value': '0.8.49'}, 2: {'key': 'App.Branch', 'value': 'test'}}
    tree = dict_to_xml_tree(d, "environment", "parameter")
    out_xml(tree, "./test.xml")


if __name__ == '__main__':
    main()

三 、读取yaml

import yaml,os
from ruamel import yaml as yaml_2

class ReadYaml():
    def __init__(self, file_path):
    # 判断文件是否存在
        if os.path.exists(file_path):
            self.file_path = file_path
        else:
            print('没有找到%s文件路径' % file_path)
        self.data = self.read_yaml()

    def read_yaml(self):
        with open(self.file_path, 'r', encoding='utf-8') as f:
            p = f.read()
            return p

    def get_data(self, key=None):
        result = yaml.load(self.data, Loader=yaml.FullLoader)
        # 判断key是否存在
        if key == None:
            return result
        else:
            return result.get(key)

    def read_yaml_o(self):
        with open(self.file_path, "r") as f:
            return yaml.safe_load_all(f.read())

    def read_yaml_def(self):
        with open(self.file_path, "r") as f:
            return yaml.safe_load(f)

    def read_yaml_list(self):
        with open(self.file_path, "r") as f:
            return list(yaml.safe_load_all(f.read()))

    def read_yaml_ruamel(self):
        with open(self.file_path) as f:
            yaml = yaml_2.YAML(typ='safe', pure=True)
            config = yaml.load(f)
            return config
if __name__ == '__main__':
    print(ReadYaml(file_path="./yaml_geshi/dict_dict.yaml").read_yaml_ruamel())
    print(ReadYaml(file_path="./yaml_geshi/dict_list.yaml").read_yaml_list())
    print(ReadYaml(file_path="./yaml_geshi/dict_dict.yaml").read_yaml_def())
    data = ReadYaml(file_path="./yaml_geshi/key_value.yaml").read_yaml_o()
    print('data:')
    print(data)
    print('利用for循环取值:')
    for d in data:
        print(d)
    print(ReadYaml(file_path="./yaml_geshi/dict_dict.yaml").get_data())
    print(ReadYaml(file_path="./yaml_geshi/dict_dict.yaml").get_data(key="input"))
    print(ReadYaml(file_path="./yaml_geshi/list_dict.yaml").read_yaml())
    print(ReadYaml(file_path="./yaml_geshi/list_dicts.yaml").read_yaml())

yml文件各种格式写法参考:python_读取yaml文件

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值