python list{dict} 格式數(shù)據(jù)對比

接口測試的時候,經(jīng)常遇到[{},{},{}]格式的數(shù)據(jù),需要diff兩套環(huán)境的返回數(shù)據(jù);

舉例:
data01=[{"path":"/Users/Library/Fonts/1610424977302997.ttf","postscriptName":"STHupo-Regular","usedPostScriptName":"STHupo","width":5,"weight":400,"style":"Regular","family":"STHupo","localizedFamily":"華文琥珀","localizedStyle":"Regular","italic":false,"monospace":false},{"path":"/Users/Library/Fonts/RobotoFlex[GRAD,XOPQ,XTRA,YOPQ,YTAS,YTDE,YTFI,YTLC,YTUC,opsz,slnt,wdth,wght].ttf","postscriptName":"RobotoFlex-Black","usedPostScriptName":"RobotoFlex-Regular","width":100,"weight":900,"style":"Black","family":"Roboto Flex","localizedFamily":"Roboto Flex","localizedStyle":"Black","italic":false,"monospace":false}]

data02=[{"path":"/Users/Library/Fonts/1610424977302997.ttf","postscriptName":"STHupo","usedPostScriptName":"STHupo","width":5,"weight":400,"style":"Regular","family":"STHupo","localizedFamily":"華文琥珀","localizedStyle":"Regular","italic":false,"monospace":false},{"path":"/Users/Library/Fonts/RobotoFlex[GRAD,XOPQ,XTRA,YOPQ,YTAS,YTDE,YTFI,YTLC,YTUC,opsz,slnt,wdth,wght].ttf","postscriptName":"RobotoFlex","usedPostScriptName":"RobotoFlex-Regular","width":100,"weight":900,"style":"Black","family":"Roboto Flex","localizedFamily":"Roboto Flex","localizedStyle":"Black","italic":false,"monospace":false},{"path":"/Users/Library/Fonts/RobotoFlex[GRAD,XOPQ,XTRA,YOPQ,YTAS,YTDE,YTFI,YTLC,YTUC,opsz,slnt,wdth,wght].ttf","postscriptName":"RobotoFlex","usedPostScriptName":"RobotoFlex-Regular","width":100,"weight":900,"style":"Black","family":"Roboto Flex","localizedFamily":"Roboto Flex","localizedStyle":"Black","italic":false,"monospace":false},{"path":"/Users/Library/Fonts/RobotoFlex[GRAD,XOPQ,XTRA,YOPQ,YTAS,YTDE,YTFI,YTLC,YTUC,opsz,slnt,wdth,wght].ttf","postscriptName":"RobotoFlex","usedPostScriptName":"RobotoFlex-Regular","width":100,"weight":900,"style":"Black","family":"Roboto Flex","localizedFamily":"Roboto Flex","localizedStyle":"Black","italic":false,"monospace":false}]

對比1:獲取data02中的重復(fù)數(shù)據(jù)

def json_object_filter(f: str, data: list[dict[str, any]] = None, ret: bool = False) -> list[dict[str, any]] or None:
    """
    從給定的json文件或數(shù)據(jù)列表過濾重復(fù)的對象
    :param f: 文件地址,文件內(nèi)容必須是json
    :param data: 數(shù)據(jù)列表
    :param ret: 是否返回數(shù)據(jù),若為False則直接打印結(jié)果,否則返回結(jié)果
    :return:
    """
    if f is None and data is None:
        raise RuntimeError("數(shù)據(jù)文件與數(shù)據(jù)列表不能都為空")

    if f is not None:
        data = json.loads((open(f, 'r')).read())

    mapping: dict[str, dict[str, any]] = {}

    for item in data:
        keys = list(item.keys())
        keys.sort()

        value = ""
        for key in keys:
            value += "{}".format(item.get(key))

        if mapping.get(value) is None:
            # 默認不要數(shù)據(jù),只統(tǒng)計value
            mapping[value] = {"count": 1}
        else:
            # 只有數(shù)據(jù)在重復(fù)時才取出數(shù)據(jù)
            if ret:
                mapping[value]["data"] = item
            elif 1 == mapping[value]["count"]:
                # 只需要打印一次
                print(item)
            mapping[value]["count"] += 1

    if ret is False:
        return None

    ret: list[dict[str, any]] = []
    for item in mapping.values():
        if item["count"] > 1:
            ret.append(item["data"])

    return ret

對比2:指定某個字段,對比data01和data02返回的字體,輸出data01和data02中不一樣的數(shù)據(jù)

def get_diff_data(f_01: str, f_02: str, data_01: list[dict[str, any]] = None, data_02: list[dict[str, any]] = None, field: str = None):
    """
    指定某個字段,對比data01和data02返回的字體,輸出data01和data02中不一樣的數(shù)據(jù)
    :param f_01: 文件地址,文件內(nèi)容必須是json
    :param f_02: 文件地址,文件內(nèi)容必須是json
    :param data_01: 數(shù)據(jù)列表
    :param data_02: 數(shù)據(jù)列表
    :param field: 需要對比的字段
    :return:
    """
    if f_01 is None and data_01 is None:
        raise RuntimeError("data01數(shù)據(jù)文件與數(shù)據(jù)列表不能都為空")
    if f_02 is None and data_02 is None:
        raise RuntimeError("data02數(shù)據(jù)文件與數(shù)據(jù)列表不能都為空")

    if f_01 is not None:
        data_01 = json.loads((open(f_01, 'r')).read())
    if f_02 is not None:
        data_02 = json.loads((open(f_02, 'r')).read())

    print("文件1中一共有{}數(shù)據(jù)".format(len(data_01)))
    print("文件2中一共有{}數(shù)據(jù)".format(len(data_02)))

    field_01_list = [i[field] for i in data_01]
    field_02_list = [i[field] for i in data_02]

    remove_duplicate_field_01_list = list(set(field_01_list))
    remove_duplicate_field_02_list = list(set(field_02_list))

    print("字段{}在文件01中一共有{}數(shù)據(jù),去重之后還有{}條數(shù)據(jù)".format(field, len(field_01_list), len(remove_duplicate_field_01_list)))
    print("字段{}在文件02中一共有{}數(shù)據(jù),去重之后還有{}條數(shù)據(jù)".format(field, len(field_02_list), len(remove_duplicate_field_02_list)))

    in_01_not_in_02 = []
    for i in remove_duplicate_field_01_list:
        if i not in remove_duplicate_field_02_list:
            in_01_not_in_02.append(i)

    in_02_not_in_01 = []
    for i in remove_duplicate_field_02_list:
        if i not in remove_duplicate_field_01_list:
            in_02_not_in_01.append(i)

    print("=========================結(jié)果===========================")
    print("字段{}在文件01中存在,但是在02中不存在:".format(field))
    print(in_01_not_in_02)

    print("字段{}在文件02中存在,但是在01中不存在:".format(field))
    print(in_02_not_in_01)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容