【Python】数据入库出库处理/list列表/数组/转字符串

 

#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
@Time    :
@Author  :
@File    :dbDataTool.py
@Version :1.0 数据入库出库处理相关工具
@Function:
"""
import hashlib

SPLIT_CHAR = ','


def getSplitCharMd5():
    md = hashlib.md5()
    md.update(SPLIT_CHAR.encode('utf-8'))
    return md.hexdigest()


class DbDataTool:
    @staticmethod
    def listToStr(listData: list) -> str:
        """
        【数据入库场景使用】list转str
        @param listData:
        @return:
        """
        newListData = [str(i).replace(SPLIT_CHAR, getSplitCharMd5()) for i in listData]
        return SPLIT_CHAR.join(newListData)

    @staticmethod
    def strToList(strData: str) -> list:
        """
        【数据出库场景使用】str转list
        @param strData:
        @return:
        """
        listData = strData.split(SPLIT_CHAR)
        newListData = [i.replace(getSplitCharMd5(), SPLIT_CHAR) for i in listData]
        return newListData


if __name__ == '__main__':
    # A,1,2,哈,c0cb5f0fcf239ab3d9c1fcd31fff1efc,da回复可见
    print(DbDataTool.listToStr(['A', '1', 2, '', ',', 'da回复可见']))

    # ['A', '1', '2', '哈', ',', 'da回复可见']
    print(DbDataTool.strToList('A,1,2,哈,c0cb5f0fcf239ab3d9c1fcd31fff1efc,da回复可见'))

 

posted @ 2022-12-09 10:16  淡怀  阅读(159)  评论(0编辑  收藏  举报