Home > Mobile >  How to compare two strings as file content in python?
How to compare two strings as file content in python?

Time:04-19

I have a file at a certain location, which was generated by python code. I want to run the same code, to generate another file, but with the same name and the same location, so it will replace the first one. Before running the code, I saved the initial file contents to a string. After running the code, I saved the final file contents to another string. How could I compare data_initial and data_final strings as file contents and to highlight exactly what words differ in those two strings? I tried like this:

    data_initial="1234"
    data_final="12345 new thing"
    first_set = set(data_initial)
    second_set = set(data_final)
    difference = first_set.symmetric_difference(second_set)

But this gives me:

difference is
 {'t', 'n', ' ', 'i', '5', 'e', 'h', 'w', 'g'}

I would like to see the words which are different, like

12345 new thing

Also if it's possible to check for each phrase that changed.

CodePudding user response:

you are using set also remember that string in python is a list even a single character. the symmetric_difference return a list of new character.

https://stackoverflow.com/a/30683765/18846844 this may satisfy what you want:

I changed the solution by using a single loop. How about this:

# First, I removed the split... it is already an array
str1 = input("Enter first string:")
str2 = input("Enter second string:")

#then creating a new variable to store the result after  
#comparing the strings. You note that I added result2 because 
#if string 2 is longer than string 1 then you have extra characters 
#in result 2, if string 1 is  longer then the result you want to take 
#a look at is result 2

result1 = ''
result2 = ''

#handle the case where one string is longer than the other
maxlen=len(str2) if len(str1)<len(str2) else len(str1)

#loop through the characters
for i in range(maxlen):
  #use a slice rather than index in case one string longer than other
  letter1=str1[i:i 1]
  letter2=str2[i:i 1]
  #create string with differences
  if letter1 != letter2:
    result1 =letter1
    result2 =letter2

#print out result
print ("Letters different in string 1:",result1)
print ("Letters different in string 2:",result2)

CodePudding user response:

If you want to get the changed words, not chars, simply transform your whole strings in lists and then sets of words by calling split() - see code below. Also, the same can be done if you want to get which sentences are changed in a paragraph, probably splitting by \n or .

str1 = "the quick brown fox jumps over the lazy dog"
str2 = "the slow brown bull jumps over the crazy frog"
wset1=set(list(str1))
wset2=set(list(str2))

##words that are in one sentence and not in the other
wset1.symmetric_difference(wset2)
{'slow', 'crazy', 'frog', 'fox', 'quick', 'bull', 'lazy', 'dog'}

##words in str1 and not in str2
wset1.difference(wset2)
{'fox', 'quick', 'lazy', 'dog'}

But if you want a more comprehensive solution, i.e. you want to know which words replaced which, you should have a look at the standard library difflib module.

  • Related