python中list和str互转,方便数据库存取

需求:list转str然后保存到数据库,取出来再转化为list使用

def deal():
    # 假设a需要保存到mysql
    a = [0, 1, 0, 0, 1]
    b = ','.join([str(x) for x in a])  # 转化为str直接写入到mysql
    print("b:", b)
    c = [int(i) for i in (b.split(','))]  # mysql中读取出来再转化为list使用
    print("c:", c)


if __name__ == '__main__':
    deal()

运行结果

b: 0,1,0,0,1
c: [0, 1, 0, 0, 1]

猜你喜欢

转载自blog.csdn.net/liuzonghao88/article/details/86570209