2016-12-03 6 views
0

私はpython difflibのドキュメントを読んでいます。 difflib.differ出力によると、次のとおりです。 意味Python Difflib - PHP Diffクラス形式のようなdiff sequnecesを取得する方法

コード「 - 」2 「」両方の配列 に共通の行を「配列に固有の1 「+」行を配列決定するラインのユニークな? 'どちらの入力シーケンスにも行がありません

Sнаđошƒаӽの回答にコメントを追加することはできません。

私は」いけないPerlのSDIFFはかなりあるか知っているが、私はこの機能を調整する必要があります。

def sdiffer(s1, s2): 
    differ = difflib.Differ() 
    diffs = list(differ.compare(s1, s2)) 

    i = 0 
    sdiffs = [] 
    length = len(diffs) 
    while i < length: 
     line = diffs[i][2:] 
     if diffs[i].startswith(' '): 
      sdiffs.append(('u', line)) 

     elif diffs[i].startswith('+ '): 
      sdiffs.append(('+', line)) 

     elif diffs[i].startswith('- '): 
      if i+1 < length and diffs[i+1].startswith('? '): # then diffs[i+2] starts with ('+ '), obviously 
       sdiffs.append(('c', line)) 
       i += 3 if i + 3 < length and diffs[i + 3].startswith('? ') else 2 

      elif diffs[i+1].startswith('+ ') and i+2<length and diffs[i+2].startswith('? '): 
       sdiffs.append(('c', line)) 
       i += 2 
      else: 
       sdiffs.append(('-', line)) 
     i += 1 
    return sdiffs 

機能上PHP Diff Class

ようになり、私がしようとしてきたし、それが値を返しますUNCHANGE、ADDED、DELETEDのいずれかを選択します。

ケース1:DELETEDは4差のケースと、より複雑で、いくつかの文字

- The good bad 
+ The good the bad 
?   ++++ 

をケース2を挿入することによって変更されたライン:ラインは、いくつかの文字を削除することによって修正される

- The good the bad 
?   ---- 
+ The good bad 

ケース3:一部の文字を削除、挿入、および/または置き換えて行を修正しました。

- The good the bad and ugly 
?  ^^ ---- 
+ The g00d bad and the ugly 
?  ^^   ++++ 

ケース4: '?' 行が削除された私は、このコードをスキップする

elif diffs[i].startswith('- '): 
     if i+1 < length and diffs[i+1].startswith('? '): # then diffs[i+2] starts with ('+ '), obviously 
      sdiffs.append(('c', line)) 
      i += 3 if i + 3 < length and diffs[i + 3].startswith('? ') else 2 

     elif diffs[i+1].startswith('+ ') and i+2<length and diffs[i+2].startswith('? '): 
      sdiffs.append(('c', line)) 
      i += 2 
     else: 
      sdiffs.append(('-', line)) 

以内に微調整する方法がわからない

- The good the bad and the ugly 
+ Our ratio is less than 0.75! 

ライン。私は、新しい行が挿入されていない場合にのみ( - )を追加し、新しい行を挿入する場合は(+)を追加したいと思います。

+0

引用符で囲まれたセクションを引用符で囲んでください。 – simbabque

答えて

0

私はPHP Diff出力のようにしたいと思います。

def sdiffer(s1, s2): 
    differ = difflib.Differ() 
    diffs = list(differ.compare(s1, s2)) 

    i = 0 
    sdiffs = [] 
    length = len(diffs) 
    sequence = 0 
    while i < length: 
     line = diffs[i][2:] 
     if diffs[i].startswith(' '): 
      sequence +=1 
      sdiffs.append((sequence,'u', line)) 

     elif diffs[i].startswith('+ '): 
      sequence +=1 
      sdiffs.append((sequence,'+', line)) 

     elif diffs[i].startswith('- '): 
      sequence +=1 
      sdiffs.append((sequence,'-',diffs[i][2:])) 
      if i+1 < length and diffs[i+1].startswith('? '): 
       if diffs[i+3].startswith('?') and i+3 < length : # case 3 
        sequence +=1 
        sdiffs.append((sequence,'+',diffs[i+2][2:])) 
        i+=3 
       elif diffs[i+2].startswith('?') and i+2 < length: # case 2 
        sequence +=1 
        sdiffs.append((sequence,'+',diffs[i+2][2:])) 
        i+=2 
      elif diffs[i+1].startswith('+ ') and i+2<length and diffs[i+2].startswith('? '): # case 1 
       sequence +=1 
       sdiffs.append((sequence,'+', diffs[i+1][2:])) 
       i += 2 
      else: # the line is deleted and inserted new line # case 4 
       sequence +=1 
       sdiffs.append((sequence,'+', diffs[i+1][2:])) 
       i+=1 
     i += 1 
    return sdiffs 
関連する問題