首页 > 编程语言 >【Python】数据入库出库处理/list列表/数组/转字符串

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

时间:2022-12-09 10:22:57浏览次数:48  
标签:return Python list CHAR listData SPLIT str 出库

 

#!/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回复可见'))

 

标签:return,Python,list,CHAR,listData,SPLIT,str,出库
From: https://www.cnblogs.com/danhuai/p/16968210.html

相关文章