【发布时间】:2016-03-11 04:32:52
【问题描述】:
我有data_pressure,这是一个 numpy 数组的列表,例如:
array([ 268752., 26222., 261152., 260958.]),
array([ 123433., 98239., 98932.]),
array([ 2893789., 872398., 92839., 9283982., 7632762., 547627.,])
每个数组都有不同的长度。
我使用 Python 的 csv 模块将这个数组列表保存到文件中:
import csv
csvfile = "csvfile.csv"
with open(csvfile, "w") as output:
writer = csv.writer(output, lineterminator='\n')
for line in data_pressure:
writer.writerow(line)
一切都像一个魅力,但当我读到它时
import csv
data = []
with open('csvfile.csv', 'rb') as csvfile:
spamreader = csv.reader(csvfile)
for row in spamreader:
data.append(np.asarray(row))
我明白了
array([ '268752.0', '26222.0', '261152.0', '260958.0']),
array([ '123433.0', '98239.0', '98932.0']),
array([ '2893789.0', '872398.0', '92839.0', '9283982.0', '7632762.0', '547627.0',])
那么任何数组中的每个值都是字符串类型而不是浮点数。
有没有办法绕过这个问题?
【问题讨论】:
-
知道了:np.asarray(row,dtype=float) 只需添加 dtype 即可。