2009-05-10 20 views
13

Bunu yapmanıza yardımcı olacak bir kitaplık biliyor musunuz?İki çok satırlı dizginin birleşik diff biçimindeki karşılaştırması nasıl yazdırılır?

Birleştirilmiş diff biçimindeki iki çok satırlı dizgiler arasındaki farkları yazdıran bir işlev yazabilirim. Bunun gibi bir şey: Bu böyle bir şey yazdırmalısınız

string1=""" 
Usage: trash-empty [days] 

Purge trashed files. 

Options: 
    --version show program's version number and exit 
    -h, --help show this help message and exit 
""" 

string2=""" 
Usage: trash-empty [days] 

Empty the trash can. 

Options: 
    --version show program's version number and exit 
    -h, --help show this help message and exit 

Report bugs to http://code.google.com/p/trash-cli/issues 
""" 

print_differences(string1, string2) 

:

def print_differences(string1, string2): 
    """ 
    Prints the comparison of string1 to string2 as unified diff format. 
    """ 
    ??? 

bir kullanım örneği şudur

--- string1 
+++ string2 
@@ -1,6 +1,6 @@ 
Usage: trash-empty [days] 

-Purge trashed files. 
+Empty the trash can. 

Options: 
    --version show program's version number and exit 

cevap

18

Bu benim nasıl çözdüğünü şudur:

def _unidiff_output(expected, actual): 
    """ 
    Helper function. Returns a string containing the unified diff of two multiline strings. 
    """ 

    import difflib 
    expected=expected.splitlines(1) 
    actual=actual.splitlines(1) 

    diff=difflib.unified_diff(expected, actual) 

    return ''.join(diff) 
18

Eğer bir göz var mı yerleşik piton modül difflib? göz bu example