无法从输入文件中读取数据

时间:2017-01-22 18:50:26

标签: python numpy file-handling

我正在尝试阅读以下输入文件,这是我的代码和输入文件click here的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时,没有显示输出。这可能是什么错误?

1 个答案:

答案 0 :(得分: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)