我有一些困难将字符串列表转换为浮点列表。我尝试了这两种方法,每个都返回不同的错误。将字符串列表(csv)转换为浮点列表
import csv
import math
unemp_reader = csv.reader(open('unemp.csv', 'rU'))
unemp_lines = list(unemp_reader)
for rows in unemp_lines: #tried this way, but error tells me indices must be integers
i = 1
for i in rows:
a = map(float, unemp_lines[i])
float_list.append(a)
print float_list
for row in unemp_lines: #tried this way but the list returned is empty
y = row[1].split(",")[1:-1]
float_list = [float(i) for i in y if i]
print float_list
你的问题是什么?一点散文就会很好。示例输入也会非常有用。还有问题。在第一个例子中'float_list'是未定义的。第二,'C'是。请先尝试运行您的示例代码。 –
值得一提的是,您应该尝试在Python中打开文件时使用 [with'语句](http://preshing.com/20110920/the-python-with-statement-by-example) 。这样更具可读性,并且可以消除文件未被关闭的可能性(即使发生异常时也是如此)。 –
嘿感谢您的快速回复,输入为 – Jonathan