2014-01-17 50 views
0

我將一些數據轉換爲csv文件,因爲它需要一段時間才能計算。該數據是一個元組列表,短版在這裏:將數據粘貼到csv中並將它們讀回numpy

[(90, 5, '69.1'), (91, 5, '59.8'), (90, 6, '48.1'), (91, 6, '41.8')] 

我做這樣說:

# Save results 
import csv 

with open('results/bake.csv','w') as out: 
    csv_out=csv.writer(out) 
    for row in data: 
     csv_out.writerow(row) 

但是當我讀回以這樣的方式

with open('results/bake.csv', 'Ur') as f: 
    data = list(tuple(rec) for rec in csv.reader(f, delimiter=',')) 


x, y, z = zip(*data) 
z = map(float, z) 
grid_x, grid_y = np.mgrid[min(x):max(x):100j, min(y):max(y):100j] 
grid_z = griddata((x, y), z, (grid_x, grid_y), method='cubic') 

我得到這個錯誤:

 grid_x, grid_y = np.mgrid[min(x):max(x):100j, min(y):max(y):100j] 
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/lib/index_tricks.py", line 188, in __getitem__ 
    step = key.step 
AttributeError: 'tuple' object has no attribute 'step' 

它必須是我在編寫和讀回數據時做錯了,因爲當我直接提供數據而沒有緩存在csv文件中時,它工作得很好。

我在做什麼錯?

回答

2

xy是字符串列表,你需要將它們轉換列出花車:

x = map(float, x) 
y = map(float, y) 
相關問題