2017-04-26 2 views
2

3つ目のファイルを作成するために2つのファイルを比較する効率的な方法を見つけるのに少し問題があります。IPを含む2つのファイルをPythonと比較する

私は、最初のファイルは、私が削除したいIPアドレスのリストであるPythonの3.6に

を使用しています。 2番目のファイルには、削除対象のIPアドレスに関連付けられているすべてのDNSレコードが含まれています。

2番目のファイルにDNSレコードがある場合は、3番目のファイルに行全体を追加します。これは私がやろうとしているものです

DNS Record Type,DNS Record,DNS Response,View 
PTR,10.10.10.234,testing.example.com,internal 
A,testing.example.com,10.10.10.234,internal 
A,dns.google.com,8.8.8.8,external 

IP 
10.10.10.234 
10.34.76.4 

これは、ファイル2のサンプルは次のとおりです。

これは、ファイル1のサンプルです。それは正確ですが、それは永遠に取っています。ファイル2には200万行、ファイル1には150,000行があります。

def create_final_stale_ip_file(): 
    PD = set() 
    with open(stale_file) as f1: 
     reader1 = csv.DictReader(f1) 
     for row1 in reader1: 
      with open(prod_dns) as f2: 
       reader2 = csv.DictReader(f2) 
       for row2 in reader2: 
        if row2['DNS Record Type'] == 'A': 
         if row1['IP'] == row2['DNS Response']: 
          PD.update([row2['View']+'del,'+row2['DNS Record Type']+','+row2['DNS Record']+','+row2['DNS Response']]) 
        if row2['DNS Record Type'] == 'PTR': 
         if row1['IP'] == row2['DNS Record']: 
          PD.update([row2['View']+'del,'+row2['DNS Record Type']+','+row2['DNS Response']+','+row2['DNS Record']]) 


    o1 = open(delete_file,'a') 
    for i in PD: 
     o1.write(i+'\n') 
    o1.close() 

ありがとうございます!

grep -xf file1 file2 

をこれはあなたにfile1の行と一致するfile2の行を含むファイルを提供します:

+0

ワイルドカード、ネットマスク、またはサブネットがないため、'10 .34.76。* 'または'10 .34。*。*'を処理する必要はありません。すべてのIPアドレスはリテラル文字列です。 (なぜこのタグが付いていますか?(タグ:サブネット)?) – smci

+0

関連:[サブネットがIP範囲内にあるかどうかは、Pythonで比較しますか?](http://stackoverflow.com/questions/22362894/compare-if-subnet-is-in -ip-range-in-python?rq = 1) – smci

答えて

-1

あなたは非常に簡単にgrepを使用してそれを行うことができます。そこから、必要な最終形式にテキストを操作する方がずっと簡単です。

0

あなたが最初setに全体のIPのファイルを読み込み、その要素がセット内に存在するかどうかをチェックすることは非常にに高速であるため、第二のファイル上のIPアドレスは、そのセットで発見されたかどうかを確認する必要があります

def create_final_stale_ip_file(): 
    PD = set() 

    # It's much prettier and easier to manage the strings in one place 
    # and without using the + operator. Read about `str.format()` 
    # to understand how these work. They will be used later in the code 
    A_string = '{View}del,{DNS Record Type},{DNS Record},{DNS Response}' 
    PTR_string = '{View}del,{DNS Record Type},{DNS Response},{DNS Record}' 

    # We can open and create readers for both files at once 
    with open(stale_file) as f1, open(prod_dns) as f2: 
     reader1, reader2 = csv.DictReader(f1), csv.DictReader(f2) 

     # Read all IPs into a python set, they're fast! 
     ips = {row['IP'] for row in reader1} 

     # Now go through every line and simply check if the IP 
     # exists in the `ips` set we created above 
     for row in reader2: 
      if (row['DNS Record Type'] == 'A' 
        and row['DNS Response'] in ips): 
       PD.add(A_string.format(**row)) 
      elif (row['DNS Record Type'] == 'PTR' 
        and row2['DNS Record'] in ips): 
       PD.add(PTR_string.format(**row)) 

    # Finally, write all the lines to the file using `writelines()`. 
    # Also, it's always better to use `with open()` 
    with open(delete_file, 'a') as f: 
     f.writelines(PD) 

ご覧のように、私も同じように、いくつかのマイナーなものを変更:writelines()

  • を使用して、最後のファイルを開くを使用してファイルに

    • 書き込みは安全
    • のために私たちは私たちのセットに一つの要素を追加しているので、代わりにPD.update()
    • 使用のPD.add()を使用するPythonの素晴らしいstr.format()少なくとも

    最後にではなく、をフォーマットよりクリーン文字列を作成するには、私が実際に希望これを複数の関数に分割します。一つはファイルの読み込み用、もう一つは読み込み用辞書の読み方用、もう一つはstale_fileprod_dnsのようなグローバル変数名の代わりに適切な引数を取ります。しかし、それはあなた次第です。

  • +0

    [csv.DictReader](https://docs.python.org/2/library/csv.html#csv。DictReader) – smci

    +0

    あなたは '** row'を意味していますか? ^^私は '.format()'でアンパックするのが好きです@smci –

    +0

    いいえ、私はそれをすべて意味します。私はDictReaderを使いませんでした。 – smci

    関連する問題