簡體   English   中英

無法從輸入文件中讀取數據

[英]Not able to read data from the input file

我正在嘗試閱讀以下輸入文件,這是我的代碼和輸入文件的 pastebin 鏈接,請單擊此處

 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)

但是當我打印 x, y, z 時,沒有顯示輸出。 這里可能的錯誤是什么?

您的縮進看起來可能是導致問題的原因。

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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM