操作

2016-05-02 9 views
1

私は.txtのこのタイプの多くのファイルを持っている:操作

name1.fits 0 0 4088.9 0. 1. 0. -0.909983 0.01386 0.91 0.01386 -0.286976 0.00379 2.979 0.03971 0. 0. 
name2.fits 0 0 4088.9 0. 1. 0. -0.84702 0..847 0.-0.250671 0.00261 3.174 0.04749 0. 0. 
#name3.fits 0 0 4088.9 0. 1. 0. -0.494718 0.01168 0.4947 0.01168 -0.185677 0.0042 2.503 0.04365 0. 0. 
#name4.fits 0 1 4088.9 0. 1. 0. -0.751382 0.01342 0.7514 0.01342 -0.202141 0.00267 3.492 0.07224 0. 0. 
name4.fits 0 1 4088.961 0.01147 1.000169 0. -0.813628 0.01035 0.8135 0.01035 -0.217434 0.00196 3.515 0.04045 0. 0. 

私は、同じタイプの別のファイルから列の値によってこれらの列の1の値を分割したいです。ここに私がこれまで持っているものがあります:

with open('4026.txt','r') as out1, open('4089.txt', 'r') as out2, \ 
    open('4116.txt', 'r') as out3, open('4121.txt', 'r') as out4, \ 
    open('4542.txt', 'r') as out5, open('4553.txt', 'r') as out6: 

    for data1 in out1.readlines(): 
     col1 = data1.strip().split() 
     x = col1[9] 

    for data2 in out2.readlines(): 
     col2 = data2.strip().split() 
     y = col2[9] 

    f = float(y)/float(x) 
    print f 

しかし、私はxのために同じ値を得ています。例えば、データの最初のセットは4089.txtであり、及び第二(4026.txt)がある場合:

name1.fits 0 0 4026.2 0. 1. 0. -0.617924 0.01749 0.6179 0.01749 -0.19384 0.00383 2.995 0.09205 0. 0. 
name2.fits 0 0 4026.2 0. 1. 0. -0.644496 0.01218 0.6445 0.01218 -0.183373 0.00291 3.302 0.05261 0. 0. 
#name3.fits 0 0 4026.2 0. 1. 0. -0.507311 0.01557 0.5073 0.01557 -0.176148 0.00472 2.706 0.07341 0. 0. 
#name4.fits 0 1 4026.2 0. 1. 0. -0.523856 0.01086 0.5239 0.01086 -0.173477 0.00279 2.837 0.05016 0. 0. 
name4.fits 0 1 4026.229 0.0144 1.014936 0. -0.619708 0.00868 0.6106 0.00855 -0.185527 0.00189 3.138 0.04441 0. 0. 

及びIは、各列の最初の要素を取って、各ファイルの第9列を分割します私は0.91/0.6179 = 1.47を取得する必要がありますが、私は0.958241758242を取得します。

答えて

1

あなたが持っているコードは、forループの最後の値を取り込み、それを分割することです。 for-loopの各段階で除算を実行して、適切な除算を行う必要があります。

より簡単な方法は、すべての値をリストに入れることです。 x = [0.0149,0.01218,..etc]y = [...]

次に、numpy(またはリストに対してforループ)を使用して2つのリストを分割します。 両者は同じサイズで作業する必要があることを覚えておいてください。

サンプルコード:

with open('4026.txt','r') as out1, open('4089.txt', 'r') as out2, open('4116.txt', 'r') as out3, open('4121.txt', 'r') as out4, open('4542.txt', 'r') as out5, open('4553.txt', 'r') as out6: 

    # Build two lists 
    x = [] 
    y = [] 

    for data1 in out1.readlines():     
     col1 = data1.strip().split() 
     x.append(col1[9]) 

    for data2 in out2.readlines():  
     col2 = data2.strip().split()  
     y.append(col2[9]) 

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

     # Make sure the denominator is not zero 
     if y[i] != 0: 
      print (1.0 * x[i])/y[i] 
     else: 
      print "Not possible" 
+0

ありがとう@Adib、これは非常に明確です。しかし、私はこの場合、 'print float(y [i])/ float(x [i])'が必要であることに注意してください。これをnumpyで行う方法を指摘できれば素晴らしいでしょう。 – JVR

+0

@JVR浮動小数点を扱う最も簡単な方法は、1つの数値に1.0を掛けることです。それを処理する方法はもっとたくさんあります:http://stackoverflow.com/questions/1267869/how-can-i-force-division-to-be-floating-point-in-python – Adib

0

あなたはこのようにそれを行うことができます:

with open('4026.txt','r') as out1, open('4089.txt', 'r') as out2: 
    x_col9 = [data1.strip().split()[9] for data1 in out1.readlines()] 
    y_col9 = [data2.strip().split()[9] for data2 in out2.readlines()] 

    if len(x_col9) != len(y_col9): 
     print('Error: files do not have same number of rows') 
    else: 
     f = [(float(y)/float(x)) for x, y in zip(x_col9, y_col9)] 
     print(f) 

それが全体の内容を読み込む必要がないので、以下のようにファイルを処理する方が良いかもしれそれらのすべてを最初にメモリに格納し、代わりに各行を一度に1行ずつ処理します。

x_col9 = [data1.strip().split()[9] for data1 in out1] 
    y_col9 = [data2.strip().split()[9] for data2 in out2] 
+0

あまりにもそれを行う方法。ありがとう。 – JVR