Wednesday, 15 August 2012

for loop - Python Text Document Translation Comparison -


Simple question I'm trying to create a "translation comparison" program that reads and compares two documents And then gives each word that is not in other documents. This is for beginners, so I am trying to avoid using obscure internal methods, even if it means less efficient code. This is what I've done so far ...

  DR def_comparison (): import again file1 = open ("desktop / file1.txt", "r") file2 = open ("desktop / File2.txt "," r ") text1 = file1.read () text2 = file2.read () text1 = re.findall (r '\ w +', text1) text2 = re.findall (r '\ w + ', Text2) in Text 2 for the item: If the item is not there, then Lesson 1: Return Item  

You can try something like this

  ####### Test Data # file1.txt = This is a test # file2.txt = This is a test # Result # # DER def_conparison (): with ("File2.txt", 'r') as F2: f2 = f2.read () with b LA ( "file1.txt", 'r') f1: f1 = f1.read (). Open as Split (). In F1, split (for): if the word is not in f2: print (word) translation_comparison ()  

This is also a good practice to use

 Open with  ("file1.txt", 'r') as f1: f1 = f1.read (). Split ()  

Use when using files with opening, when you are not using it, then the file will be closed to release the python and manage the memory. Very good but it is always good to ensure that you release or call it

  file1.close ()  

when you are doing Are there.


No comments:

Post a Comment