字符串转换为数组 [英] convert a string to an array

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

问题描述

你如何将字符串转换到一个数组?说字符串是像文本=A,B,C。结果
转换后,文本== [A,B,C] ,希望正文[0] ==一个正文[1] == b

How do you convert a string into an array? say the string is like text = "a,b,c".
After the conversion, text == [a,b,c] and hopefully text[0] == a, text[1] == b?

感谢您

推荐答案

这样的:

>>> text = 'a,b,c'
>>> text = text.split(',')
>>> text
[ 'a', 'b', 'c' ]

另外,你可以使用的eval()如果您信任该字符串是安全的:

Alternatively, you can use eval() if you trust the string to be safe:

>>> text = 'a,b,c'
>>> text = eval('[' + text + ']')

这篇关于字符串转换为数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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