0

I am doing a Python task and I am trying to compare two lines of code and check a certain sub-string on both lines.

Let us have for example: I am reading two files that have these lines

<Field Name="t" Description=" "/> First file

<Field Name="ta" Description="description1"/> Second file

What I am trying to accomplish is to compare if there is difference between those two descriptions.

I have tried using functions like any or next but they return booleans and they check difference for the whole lines,where as here , I need only to check the type of substring I want to search for.

Important part to mention is that I am not using regular split or other functions to define a start index and end index, and how many characters I want. I have a list of includes that has the current values:

matches=['Description="',etc]

Also I CAN have differences in other parts of the string such as Field Name="something" , like how in the above example those 2 files have different field names, but we only care about the description.

And now for the question. Is it possible to use that list element to look for that substring in both lines of the two files, and check if they are different or not? I need to check only for the specific matches I have in this matches list.

1 Answer 1

1

This code below will do the trick:

import xml.etree.ElementTree as ET 

s1 = '<Field Name="t" Description=" "/>'
s2 = '<Field Name="ta" Description="description1"/>'
s3 = '<Field Name="Doesnt matter that this is diffrent" Description="description1"/>'

def compare_strings(attribute, strings):
    des = []
    for string in strings:
        tree = ET.fromstring(string)
        des.append(tree.attrib[attribute])

    if des and all(x == des[0] for x in des):
        return True #They are the same
    else:
        return False #They are not the same
    


print("Are s1 adn s2 descriptions the same? ", compare_strings("Description", [s1, s2]))
print("Are s2 adn s3 descriptions the same? ", compare_strings("Description", [s2, s3]))
  1. Use xml.etree.ElementTree for parsing this kind of Str objects.
  2. The compare_strings function takes a list of strings as an argument and name of the attribute that it should compare.
  3. Then it iterates through all of them and saves the 'Description' attribute.
  4. Lastly it checks if all the descriptions are the same. If they are the same: returns True, if they are not: returns False
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks! I tried it out and I think this will do the job for what I need
But what about if there are spaces in between? If I want to check Field Name for example? Since Description is a 1 word and takes it as key, but in Field Name, takes only Name as key
Then the string doesn't follow XML standard. And you should consider formatting it better, for example: fieldName or field_name.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.