简体   繁体   English

如何在python中将变量转换为数组?

[英]how to transform in python a variable in to array?

i have a STRING VARIABLE like this:我有一个这样的字符串变量:

x = "[str1, str2]"

look, variable x IS NOT an array看,变量 x 不是数组

when i do:当我做:

print x
>>> [str1, str2]

there is a direct way to transform variable "x" from a string variable into an array?有一种直接的方法可以将变量“x”从字符串变量转换为数组吗? Thanks for your answers.感谢您的回答。

If you really insist, you can do如果你真的坚持,你可以做

x = eval("['a', 'b']")

However make sure to read on security issues with using eval但是请务必阅读有关使用eval安全问题

A better solution would be to understand the format of your data and use a parser to get the data you want更好的解决方案是了解您的数据格式并使用解析器来获取您想要的数据

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM