Python comparison to file differences

Source: Internet
Author: User
Tags diff

Cat Standart

1

2

3

5


Cat Node1.txt

1

2

5

6


Cat Node2.txt

1

2

3

4

5

6


Cat Node3.txt

1

7

8

9


The desired output results are:

Node1:3

Node2:

Node3:2 3 5

#!/usr/bin/python# -*- coding:utf-8 -*-def diff (x, y):         list_x = []        list_y = []         file = open (x, ' R ')          for line in file.readlines ():                 line = line.strip (' \ n ')                  list_x.append (line)          file = open (Y, ' R ')         for  Line in file.readlines ():                 line = line.strip (' \ n ')               &nBsp;  list_y.append (line)         list_x = set (list _x)         list_y = set (list_y)          diff_list = list (List_x.difference (list_y))          diff_str =  ', '. Join (diff_list)          print  '%s:%s '  %  (y,diff_str) file0 =  ' standart ' file1 =  ' node1.txt ' file2  =  ' node2.txt ' file3 =  ' Node3.txt ' diff (file0,file1) diff (file0,file2) diff (File0,file3)


This article is from the "Eight Miles" blog, so be sure to keep this source http://5921271.blog.51cto.com/5911271/1580104

Python comparison to file differences

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.