2016-06-14 50 views
0

我想比較python中的兩個不同文件。它們包含具有概率的行,並且每行在文件末尾都有一個in id。我需要計算每個ID的比例。問題是每一行可能包含不同數量的概率,最終每個文本的行數不同。我成功地創建了一個腳本,只用一行比較兩個文件,但我不知道如何爲文本中的每一行執行此操作。以下是我的腳本到目前爲止:在python中逐行比較兩個文件

#!/usr/bin/python 
import math 
import operator 
f = open('output.txt','w') 
file1= open("test.ppx1","r") 
file2= open("test.prob1","r") 
words = list(file1.read().split()) 
words2 = list(file2.read().split()) 
id1=words[-1] 
id2=words2[-1] 
words.remove(id1) 
words2.remove(id2) 
words[:]=[x[:12] for x in words] 
words2[:]=[x[:12] for x in words2] 
words=map(float,words) 
words2=map(float,words2) 
words=[math.log(y,10) for y in words] 
words2=[math.log(y,10) for y in words2] 
words=sum(words) 
words2=sum(words2) 
ratio= words-words2 
print >>f, id1,words, words2,ratio 
+0

您能否顯示兩個文件的示例? – NTAWolf

+0

2.506201e-08 2.346253e-02 1.282699e-02 3.336181e-05 1.821797e-07 1.424501e-07 utt-0000000001 2.506201e-08 2.346253e-02 1.282699e-02 3.336181e-05 1.821797e-07 1.424501 e-07 utt-0000000002 2.506201e-08 2.346253e-02 1.282699e-02 3.336181e-05 1.821797e-07 1.424501e-07 utt-0000000003第一個文件 – oezlem

+0

2.506201e-08 2.346253e-02 1.282699e- 02 3.336181e-05 1.821797e-07 1.424501e-07 2.506201e-08 1.821797e-07 1.424501e-07 utt-0000000001 2.506201e-08 2.346253e-02 1.282699e-02 3.336181e-05 1.821797e-07 1.424501 e-07 utt-0000000002 2.506201e-08 2.346253e-02 1.282699e-02 3.336181e-05 1.821797e-07 1.424501e-07 1.424501e-07 1.424501e-07 utt-0000000003 – oezlem

回答

1

您可能想壓縮兩個文件的讀取內容並遍歷它們。請注意,當您使用zip時,對於不同行長度的文件,所有文件的最小文件長度將變爲最短。請參閱Documentation of zip builtin function

import math 


file_list = [] 

with open("test.ppx1", "r") as file1: 
    file_list.append(file1.readlines()) 

with open("test.prob1", "r") as file2: 
    file_list.append(file2.readlines()) 

with open('output.txt', 'w') as file_out: 
    for file1_str, file2_str in zip(*file_list): 
     file1_list = file1_str.split() 
     file2_list = file2_str.split() 
     id1, id2 = file1_list.pop(), file2_list.pop() 
     p1 = map(float, file1_list) 
     p2 = map(float, file2_list) 
     p1 = map(lambda y: math.log(y, 10), p1) 
     p2 = map(lambda y: math.log(y, 10), p2) 
     s1, s2 = sum(p1), sum(p2) 
     ratio = s1 - s2 
     file_out.write("{} {} {} {}".format(id1, s1, s2, ratio)) 
+0

Traceback(最近呼叫最後一個): 文件「log2.py」,第11行,在 p1 = map(float,file1_list.split()) ValueError:無法將字符串轉換爲float:。 我有這個錯誤 – oezlem

+0

我修改了答案。我從來沒有想過這些ID不能轉換成浮動。 –