如何将整个列表转换为字符串,然后将字符串转换回整个列表 [英] how to turn a whole list into a string and a string back to a whole list

查看:75
本文介绍了如何将整个列表转换为字符串,然后将字符串转换回整个列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我想将列表转换为字符串,但将整个列表

hello i want to turn a list into string but the whole list

a = [1,2,3]

我希望它进入

b = '[1,2,3]'

我也想从那里回来,所以我从

i also want it back from there so i get from

b = '[1,2,3]'

c = [1,2,3]

这样我就可以再次将其用作普通列表我需要以这种方式进行转换,因为我有一个json遇到问题的列表,因此我希望将其转换为字符串,然后当我需要json文件中的列表时,我希望将其作为列表返回.

so i can use it as normal list again i need to convert it this way because i have a list that json got a probleme with and so i want it into a string and then when i need the list out of the json file i want it back as a list.

推荐答案

列出字符串很容易:

>>> str([1,2,3])
'[1,2,3]'

或者,按照约翰·戈登的说法,

Or, as per John Gordon,

>>> json.dumps([1,2,3])
'[1,2,3]'

您可以使用json.loads转换回去:

And you can convert back using json.loads:

>>> json.loads('[1,2,3]')
[1,2,3]

这篇关于如何将整个列表转换为字符串,然后将字符串转换回整个列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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