在Jupyter Notebook中处理ASCII文件

时间:2018-01-12 06:28:57

标签: python matplotlib ascii jupyter-notebook

我刚开始用python编码,我想从ASCII文件中提取并绘制一些数据。 我在Jupyter Notebook上使用以下代码来导入和读取文件:

filename = input("Insert file name: ")
openfile = open(filename,'r')
readfile = openfile.read()
print(readfile) 

输出如下:

Insert file name: Example_File.asc

row 1

row n


row 47
    1    0.000    168.38     0.0000                                                 0.0000     0.0000                      
    2    0.020    169.42     0.0002                           0.0000               -0.0001     0.0002                      
    3    0.040    170.47     0.0006                           0.0000    -2.9038     0.0000     0.0006                      
    4    0.060    171.47     0.0002     0.0000                0.0000     1.0652    -0.0008     0.0002                      
    5    0.080    172.38     0.0007    -0.0001    -8.7283     0.0000     1.3343    -0.0006     0.0007                      
    6    0.100    173.38     0.0011     0.0000    -3.9063     0.0000     2.0894    -0.0005     0.0011                      
    7    0.120    174.33     0.0010     0.0000    -4.6450     0.0000     1.2769    -0.0010     0.0010                      
    8    0.140    175.33     0.0005     0.0000    -3.2740     0.0000     0.8283    -0.0017     0.0005                      
    9    0.160    176.38     0.0009     0.0000    -2.3600     0.0000     0.9500    -0.0017     0.0008                      
   10    0.180    177.38     0.0010    -0.0001    -8.3284     0.0000     0.9759    -0.0019     0.0010                      
   11    0.200    178.38     0.0007     0.0000    -5.7496     0.0000     0.8248    -0.0025     0.0007                      
  254    5.060    471.81     6.1288     0.0220     0.3673     0.0202    -0.0050     6.0529     6.1338                      
  255    5.080    472.77     6.7917                           0.0198    -0.0046     6.7154     6.7967                      
  256    5.100    473.69     7.3207                           0.0192    -0.0044     7.2439     7.3255                      
  257    5.120    474.71     7.9230                           0.0191    -0.0041     7.8458     7.9277                      
  258    5.140    475.69     8.2525                           0.0205    -0.0038     8.1754     8.2576                      
  259    5.167    477.38     8.8266                                     -0.0035     8.7492     8.8319                      

然后我在做:

for line in readfile:
    line = line.strip()
    columns = line.split()
    c1 = float(columns[1])
    c2 = float(columns[2])
    c3 = float(columns[3])
    c4 = float(columns[4])
    c5 = float(columns[5])
    c6 = float(columns[6])
    c7 = float(columns[7])
    c8 = float(columns[8])
    c9 = float(columns[9])
    c10 = float(columns[10])

但是我收到了以下错误:

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-11-f88eb57daf30> in <module>()
      2     line = line.strip()
      3     columns = line.split()
----> 4     c1 = float(columns[1])
      5     c2 = float(columns[2])
      6     c3 = float(columns[3])

IndexError: list index out of range

有人可以帮我吗? 提前谢谢。

0 个答案:

没有答案