2017-07-07 4 views
0

私は1000 +行のタイムスタンプ列を含む2つのファイルを持っています。ファイルf1の行は、ファイルf2の行に関連しています。私は可能な限り最良の方法ですべての対応する行に対して[f1 nth row,f2 nth row]を実行するPythonスクリプトを望んでいました。ありがとう!関連するタイムスタンプ列を持つ2つのファイルの内容をPythonでリストを作成するために

f1: 
05:43:44 
05:59:32 

f2: 
05:43:51 
05:59:39 

[05:43:44,05:43:51]、[05:59:32,05:59:39] ....

答えて

1

あなたは次のような何かを行うことができます:

f1_as_list = open(f1).readlines() # get each line as a list element 
f2_as_list = open(f2).readlines() 
zipped_files = zip(f1_as_list, f2_as_list) # zip the two lists together 
0

これはおそらく最も直感的なアプローチです。

#!/usr/bin/python3 
with open("f1.txt") as f1: 
    with open("f2.txt") as f2: 
    for row1 in f1: 
     for row2 in f2: 
     print("%s %s" % (row1.strip(), row2.strip())) 

リストの理解を優先する人もいますが、非パイソン主義者は直感的ではないと考えるかもしれません。

with open("f1.txt") as f1: 
    with open("f2.txt") as f2: 
    print("\n".join([ 
     "%s %s" % (row1.strip(), row2.strip()) 
     for row1 in f1 
     for row2 in f2 
    ])) 
関連する問題