在python中比较文件内容的有效方法

时间:2018-02-20 06:43:02

标签: python python-2.7

我有像下面这样的文件输出我称之为file1:

 #number of apples
 apple = 10
 #Number of apples in quality
 apple_quality = 5 2 3
 #Number of Banana
 banana = 20
 #Number of banana in quality
 banana_quality = 10 5 5

我需要比较文件输出,下面是示例代码(未测试)是否正确的方法来比较它或任何简单的方法来实现这一点?

 d = {}
 with open ("file1", "r") as f:
     for line in f:
         if "=" not in line:
             continue
         key, value = line.strip().split("=", 1)
         d[key] = value
 if (d['apples'].strip() == "10" and
     d['apple_quality'].strip() == "5 2 3" and
     d['banana'].strip() == "20" and
     d['banana_quality'].strip() == "10 5 5" ):
     print "True"
 else:
     print "False"

0 个答案:

没有答案