python 字符串 赋值给list
怎么把字符串"['1.0','0.0','0.0','1.0']"变为list类型['1.0','0.0','0.0','1.0'],谢谢!我从其他接口得到的是这样一个字...
怎么把字符串"['1.0','0.0','0.0','1.0']"变为list类型['1.0','0.0','0.0','1.0'],谢谢!
我从其他接口得到的是这样一个字符串,怎样把它直接转换为list呢?
“[['sh600275','武昌鱼','4.68','10.12','1','0.05','0','11.84'],['sz002105','信隆实业','11.06','10.05','1','18.01','0','7.12'],['sz300402','宝色股份','13.81','10.04','1','1','0','4.37'],['sh603169','兰石重装','5.70','10.04','1','0.58','0','3.45'],['sz002528','英飞拓','15.03','10.03','1','0.2','146.19','3.26']]” 展开
我从其他接口得到的是这样一个字符串,怎样把它直接转换为list呢?
“[['sh600275','武昌鱼','4.68','10.12','1','0.05','0','11.84'],['sz002105','信隆实业','11.06','10.05','1','18.01','0','7.12'],['sz300402','宝色股份','13.81','10.04','1','1','0','4.37'],['sh603169','兰石重装','5.70','10.04','1','0.58','0','3.45'],['sz002528','英飞拓','15.03','10.03','1','0.2','146.19','3.26']]” 展开
2个回答
展开全部
import ast
x ="[['sh600275','武昌鱼','4.68','10.12','1','0.05','0','11.84'],['sz002105','信隆实业','11.06','10.05','1','18.01','0','7.12'],['sz300402','宝色股份','13.81','10.04','1','1','0','4.37'],['sh603169','兰石重装','5.70','10.04','1','0.58','0','3.45'],['sz002528','英飞拓','15.03','10.03','1','0.2','146.19','3.26']]"
x = ast.literal_eval(x)
print x
展开全部
#coding: utf-8
import re
s = "['1.0','0.0','0.0','1.0']"
s = re.search('\[(.*)\]',s).group(1)
l = []
for x in s.replace("'",'').split(','):
try:
l.append(float(x))
except:
pass
print l
或者:
s = "['1.0','0.0','0.0','1.0']"
l= [float(x) for x in eval(s)]
print l
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询