-1

As per my requirement, I wish to match two text files line by line in Python on Windows platform. for example I have the following text files:

File1:

My name is xxx

command completed successfully.

My mother's name is yyy

My mobile number is 12345

the heavy lorry crashed into the building at midnight

lorry eat in the faculty a red apple

File2:

My name is xxx

command . successfully.

The name of my mother is

what a heavy lorry it is that crashed into the building

lorry eat an apple in the faculty

I apologize for not being clear enough so my problem is how can i align a script movie with its subtitles, i writ the following code in Python but it's not enough to get the alignement from the two text files:

 # Open file for reading in text mode (default mode)
f1 = open('F:/CONTRIBUTION 2017/SCRIPT-SUBTITLES CODES/Script Alignement Papers/f1.txt','r')
f2 = open('F:/CONTRIBUTION 2017/SCRIPT-SUBTITLES CODES/Script Alignement Papers/f2.txt','r')

#Print confirmation
# print("-----------------------------------")
#print("Comparing files ", " > " + fname1, " < " +fname2, sep='\n')
# print("-----------------------------------")

# Read the first line from the files
f1_line = f1.readline()
f2_line = f2.readline()

# Initialize counter for line number
line_no = 1

# Loop if either file1 or file2 has not reached EOF
while f1_line != '' or f2_line != '':

    # Strip the leading whitespaces
    f1_line = f1_line.rstrip()
    f2_line = f2_line.rstrip()

    # Compare the lines from both file
    if f1_line != f2_line:

        # If a line does not exist on file2 then mark the output with + sign
        if f2_line == '' and f1_line != '':
            print("=================================================================")
            print("=================================================================")
            print("line does not exist on File 2 ====================")
            print("=================================================================")
            print(">+", "Line-%d" % line_no, f1_line)
        # otherwise output the line on file1 and mark it with > sign
        elif f1_line != '':

            print("=================================================================")
            print("=================================================================")
            print("otherwise output the line on file1 ====================")
            print("=================================================================")
            print(">", "Line-%d" % line_no, f1_line)

        # If a line does not exist on file1 then mark the output with + sign
        if f1_line == '' and f2_line != '':
            print("=================================================================")
            print("=================================================================")
            print("=line does not exist on File 1 ====================")
            print("=================================================================")
            print("<+", "Line-%d" % line_no, f2_line)
        # otherwise output the line on file2 and mark it with < sign
        elif f2_line != '':
            print("=================================================================")
            print("=================================================================")
            print("otherwise output the line on file2 ====================")
            print("=================================================================")
            print("<", "Line-%d" %  line_no, f2_line)

        # Print a blank line
        print()

    #Read the next line from the file
    f1_line = f1.readline()
    f2_line = f2.readline()


    #Increment line counter
    line_no += 1

# Close the files
f1.close()
f2.close()

If can anyone help to do this matching, i would be very grateful.

ucmou
  • 79
  • 1
  • 1
  • 8
  • If you're just wanting to check if files are identical the answers provided should be enough. For anything more complex I suggest you take a look at https://docs.python.org/3.5/library/difflib.html – Jack Evans Jan 11 '17 at 10:39
  • What should the outcome of this comparison be? What is the expected output here? – Ma0 Jan 11 '17 at 10:42
  • It looks like you want us to write some code for you. While many users are willing to produce code for a coder in distress, they usually only help when the poster has already tried to solve the problem on their own. A good way to demonstrate this effort is to include the code you've written so far, example input (if there is any), the expected output, and the output you actually get (output, tracebacks, etc.). The more detail you provide, the more answers you are likely to receive. Check the [FAQ](http://stackoverflow.com/tour) and [How to Ask](http://stackoverflow.com/questions/how-to-ask). – TigerhawkT3 Jan 11 '17 at 10:45
  • I apologize for not being clear enough so my problem is how can I align script movie with its subtitles, I write the following code in Python but it's not enough to get the alignment from the two text files. (you can find the code in the edited question) – ucmou Jan 11 '17 at 11:16

2 Answers2

0

It would be good to post code you tried writting. This feels like we are doing your homework and makes you look lazy. That being said, take a look at the following:

with open(file1, 'r') as f1, open(file2, 'r') as f2:
    if f1.readlines() == f2.readlines():
        print('Files {} & {} are identical!'.format(file1, file2))

PS: This checks whether the files are identical. If you want something like a logical comparison you have to do some research first.

Ma0
  • 15,057
  • 4
  • 35
  • 65
  • I apologize for not being clear enough so my problem is how can I align script movie with its subtitles, I write the following code in Python but it's not enough to get the alignment from the two text files. (you can find the code in the edited question) – ucmou Jan 11 '17 at 11:16
0

One possible way is to store the lines of the file in a list and then compare the lists.

lines_of_file1 = []
file = open("file1.txt","r")
line = 'sample'
while line != '':
    line = file.readline()
    lines_of_file1.append(line)
file.close()
lines_of_file2 = []
file = open("file2.txt","r")
line = 'sample'
while line != '':
    line = file.readline()
    lines_of_file2.append(line)
file.close()
same = True
for line1 in lines_of_file1:
     for line2 in lines_of_file2:
        if line1 != line2:
            same = False
            break
if same:
    print("Files are same")
else:
    print("Files are not same")

Hope that helps.

Aamir Khan
  • 324
  • 2
  • 10
  • I apologize for not being clear enough so my problem is how can I align script movie with its subtitles, I write the following code in Python but it's not enough to get the alignment from the two text files. (you can find the code in the edited question) – ucmou Jan 11 '17 at 11:16