繁体   English   中英

如何在 Python 中将字符串变量转换为数组?

[英]How to convert a string variable to an Array in Python?

我有一个这样的字符串值: {"id": "312749", "315082", "316379", "316648", "320454", "321766"}我希望它在 python 中被读取为数组。 ids 作为表名,ids 值作为数组的变量。 我正在学习 python 我想我一定没有使用正确的术语进行搜索,我没有找到解决方案。 你可以帮帮我吗?

假设 OP 真的意味着一个列表而不是一个 [numpy] 数组,这将起作用:

import re
from numpy import array
STRING = '{"ids": 319242, 322456, 327484"}'
LIST = [int(n) for n in re.findall('\d+', STRING)]
print(LIST)
ARRAY = array(LIST)
print(ARRAY)

输出:

[319242, 322456, 327484]
[319242 322456 327484]

编辑:

添加了 numpy 数组

暂无
暂无

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

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