2016-12-03 93 views
0

我正在閱讀python difflib文檔。根據difflib.differ輸出是:Python Difflib - 如何獲得像PHP Diff類型的diff sequneces

代碼含義 「 - 」線特有的序列1 「+」獨特線到序列2 「」常見的兩種序列 「線? '線不存在於任何輸入序列中

我也閱讀了這個問題在stackoverflow Python Difflib - How to Get SDiff Sequences with "Change" Op但無法添加評論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。 DELETED是具有4的區別的情況下這是更復雜的:

案例1:通過插入一些字符

- 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: '?' 該行刪除

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

我不知道如何將這種代碼

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)) 

跳過內調整線。我只想在沒有插入新行的情況下追加( - ),如果插入新行,則追加(+)。

+0

請在您的問題中標記爲引用的部分作爲引號。 – simbabque

回答

0

我想我已經做了我想要的PHP差分輸出。

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