dimanche 23 septembre 2018

Distinguishing strings in a list with only blank spaces

I am trying to create an if statement that can distinguish between two strings in a list, where the only difference is the amount of blank space before them.

For some reason, I can't get any of these to work.

with open(os.path.dirname(os.path.realpath(__file__)) + "\defaults\handling.meta") as file:
contents = file.readlines()

for index, item in enumerate(contents):
    if ("    </Item>" in item) and (index > old_start_lines[0]):

This does not distinguish between

        </Item>

(8 blank spaces) and

    </Item>

(4 blank spaces). Adding a length check like so produces an error:

    and (len(contents[index]) == 11):

These other two checks also result in an undistinguished result and the same error respectively.

    if (item.startswith("    </Item>")) and (index > old_start_lines[0]):
    if ("    </Item>" == item) and (index > old_start_lines[0]):

The error is:

IndexError: list index out of range

Here is an example of the text file -> list that I am working from: https://pastebin.com/Vr87UXHF

Thanks,

Alfie

Aucun commentaire:

Enregistrer un commentaire