2016-12-03 29 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