Python-要列出的字符串 [英] Python - String to list

查看:88
本文介绍了Python-要列出的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将列表转换为字符串,然后将字符串恢复为列表.有一种实现这种行为的python方法吗?

I need cast a list to a string and get back the string to a list. There's a python way to make this behavior?

l1 = ['aa','bb','cc']
s = str(l1)
l2 = cast_string_to_list(s)
print l2
"['aa','bb','cc']"

推荐答案

使用类似json的序列化库:

import json

l1 = ['aa','bb','cc']
s = json.dumps(l1)
l2 = json.loads(s)

print s
print l1 == l2

这篇关于Python-要列出的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆