2017-01-22 101 views
-1

我想讀下面輸入文件中讀取數據,這裏是我的代碼,並輸入文件click here的引擎收錄鏈接:無法從輸入文件

1  42.5340919495 4.22926330566 
2  41.3636322021 2.87980079651 
3  38.7423553467 3.40052604675 
4  36.631401062 2.33657073975 
5  35.0620422363 3.57421207428 

這是怎麼了產生輸入文件:

with open('position/output.dat','a') as output: 

    for i in range(0, len(position_mean)): 

     output.write('{}\t{}\t{}'.format(i+1, position_mean[i] , position_std[i]) + "\n") 

output.close() 

這是我正在讀輸入文件:

with open("position/output.dat", 'r') as f: 
    x = [] 
    y = [] 
    z = [] 
    for line in f: 
     if not line.strip() or line.startswith('@') or line.startswith('#'): 
      continue 
     row = line.split("\t") 
     x.append(float(row[0])) 
     y.append(float(row[1])) 
     z.append(float(row[2])) 

x = np.asarray(x) 
y = np.asarray(y) 
z = np.asarray(z) 

但是當我的PR int x,y,z,沒有輸出顯示。這裏可能有什麼錯誤?

+0

Pease顯示實際的縮進! – schwobaseggl

+1

使用正確的縮進,這個*會*輸出'x','y'和'z'(在添加適當的打印語句並將'line.split(「\ t」)''改爲'line.split )')。我正在投票結束這個問題,這是無法複製的問題。 – Tagc

回答

0

您的縮進看起來可能會導致問題。

with open("stack_test.txt", 'r') as f: 
    x = [] 
    y = [] 
    z = [] 
    for line in f: 
     if not line.strip() or line.startswith('@') or line.startswith('#'): 
      continue 
     row = line.split("\t") 
     x.append(float(row[0])) 
     y.append(float(row[1])) 
     z.append(float(row[2])) 

    x = np.asarray(x) 
    y = np.asarray(y) 
    z = np.asarray(z) 
+0

我83%確定這是OP實際運行的代碼,他的代碼中的縮進問題僅僅是他的文章中的格式錯誤。 – Tagc

+0

@Tagc對不起,我的網絡被關閉了。無法回覆。你是正確的83%,但它是一個後期格式問題。 –