【发布时间】:2014-03-07 06:17:34
【问题描述】:
我正在尝试读取一个 csv 文件并创建一个包含数据行的数组。这是我的代码:
import csv
def main():
a = range(4)
x = 0
with open('test.csv', 'rb') as csvfile:
spamreader = csv.reader(csvfile, delimiter=' ', quotechar='|')
for row in spamreader:
a[x] = [float(x) for x in row.split()]
x += 1
print a
输出:
[['13,4.2,2.4,5,6.4'], ['14,3.2,3.4,5.6,7.2'], ['15,8.5,3.7,8.5,0.75'], ['16,5.4,8.3,3.5,5.4']]
如何将这些数组从 1 个字符串转换为浮点数组?
【问题讨论】:
-
当你的数据用逗号分隔时,为什么你有
delimiter=' '?