我正在从.csv格式的设备中收集测量值。每个单元格都有一组用逗号分隔的值。示例如下,
cell = 0.0871666666666667,0.0866,0.0862,0.083,0.0834,0.0812857142857143,0.08075,0.0782,0.0751,0.0748181818181818,0.0737142857142857,0.072,0.07,0.07,0.0680833333333333,0.068,0.0654615384615385,0.0641818181818182,0.0616428571428571,0.0615714285714286,0.0599411764705882,0.0587857142857143,0.0573478260869565,0.0554285714285714,0.0545652173913044,0.0523225806451613,0.0511818181818182,0.0492553191489362,0.0480322580645161,
上方的单元格为object
格式。
我想将其转换为
desired_output = [0.0871666666666667,0.0866,0.0862,0.083,0.0834,0.0812857142857143,0.08075,0.0782,0.0751,0.0748181818181818,0.0737142857142857,0.072,0.07,0.07,0.0680833333333333,0.068,0.0654615384615385,0.0641818181818182,0.0616428571428571,0.0615714285714286,0.0599411764705882,0.0587857142857143,0.0573478260869565,0.0554285714285714,0.0545652173913044,0.0523225806451613,0.0511818181818182,0.0492553191489362,0.0480322580645161]
我做了以下转换。但这并没有实现我上面想要的。我的代码如下。
actual_output = list(cell)
print(actual_output)
actual_output = ['0', '.', '0', '9', '3', ',', '0', '.', '0', '9', '1', '4', '2', '8', '5', '7', '1', '4', '2', '8', '5', '7', '1', '4', ',', '0', '.', '0', '9', '0', '6', '6', '6', '6', '6', '6', '6', '6', '6', '6', '6', '6', '7', ',', '0', '.', '0', '8', '7', '1', '6', '6', '6', '6', '6', '6', '6', '6', '6', '6', '6', '7', ',', '0', '.', '0', '8', '6', '6', ',', '0', '.', '0', '8', '6', '2', ',', '0', '.', '0', '8', '3', ',', '0', '.', '0', '8', '3', '4', ',', '0', '.', '0', '8', '1', '2', '8', '5', '7', '1', '4', '2', '8', '5', '7', '1', '4', '3', ',', '0', '.', '0', '8', '0', '7', '5', ',', '0', '.', '0', '7', '8', '2', ',', '0', '.', '0', '7', '5', '1', ',', '0', '.', '0', '7', '4', '8', '1', '8', '1', '8', '1', '8', '1', '8', '1', '8', '1', '8', ',', '0', '.', '0', '7', '3', '7', '1', '4', '2', '8', '5', '7', '1', '4', '2', '8', '5', '7', ',', '0', '.', '0', '7', '2', ',', '0', '.', '0', '7', ',', '0', '.', '0', '7', ',', '0', '.', '0', '6', '8', '0', '8', '3', '3', '3', '3', '3', '3', '3', '3', '3', '3', '3', ',', '0', '.', '0', '6', '8', ',', '0', '.', '0', '6', '5', '4', '6', '1', '5', '3', '8', '4', '6', '1', '5', '3', '8', '5', ',', '0', '.', '0', '6', '4', '1', '8', '1', '8', '1', '8', '1', '8', '1', '8', '1', '8', '2', ',', '0', '.', '0', '6', '1', '6', '4', '2', '8', '5', '7', '1', '4', '2', '8', '5', '7', '1', ',', '0', '.', '0', '6', '1', '5', '7', '1', '4', '2', '8', '5', '7', '1', '4', '2', '8', '6', ',', '0', '.', '0', '5', '9', '9', '4', '1', '1', '7', '6', '4', '7', '0', '5', '8', '8', '2', ',', '0', '.', '0', '5', '8', '7', '8', '5', '7', '1', '4', '2', '8', '5', '7', '1', '4', '3', ',', '0', '.', '0', '5', '7', '3', '4', '7', '8', '2', '6', '0', '8', '6', '9', '5', '6', '5', ',', '0', '.', '0', '5', '5', '4', '2', '8', '5', '7', '1', '4', '2', '8', '5', '7', '1', '4', ',', '0', '.', '0', '5', '4', '5', '6', '5', '2', '1', '7', '3', '9', '1', '3', '0', '4', '4', ',', '0', '.', '0', '5', '2', '3', '2', '2', '5', '8', '0', '6', '4', '5', '1', '6', '1', '3', ',', '0', '.', '0', '5', '1', '1', '8', '1', '8', '1', '8', '1', '8', '1', '8', '1', '8', '2', ',', '0', '.', '0', '4', '9', '2', '5', '5', '3', '1', '9', '1', '4', '8', '9', '3', '6', '2', ',', '0', '.', '0', '4', '8', '0', '3', '2', '2', '5', '8', '0', '6', '4', '5', '1', '6', '1', ',']
请提出任何建议。
一种建议的方法:
x = 2.809,2.812,2.813,2.808,2.804
print(x)
str_list = x.split(',')
print(str_list)
flt_lst = [float(a) for a in str_list]
print(flt_lst)
输出为:
x = 2.809,2.812,2.813,2.808,2.804
str_list = ['2.809', '2.812', '2.813', '2.808', '2.804']
string could not be converted to float
答案 0 :(得分:1)
您的变量cell
是一个字符串,因此您的list
调用使其成为可迭代的。试试
str_lst = cell.split(',')
float_lst = [float(x) for x in str_lst]