0

I am attempting to create a small iterative string match function in Python. I am getting stumped as the first set of matching strings is caught, but the second set of matching strings is not. I added a string conversion to ensure the objects were strings. It is also my understanding that the == operator matches values, not objects. (Correct me if my terminology is wrong). Any help would be appreciated.

#!/usr/bin/python


import re


NameLG_file = open("Name2.txt", "r")
NameSM_file = open("Inc2.txt", "r")
SMList = []
LGList = []


# Assign LG to List and Format
for a in NameLG_file:
    a = a.strip('\n')
    a = a.replace('\"', '')
    a = str(a)
    LGList.extend([a])


# Assign SM to list and format
for c in NameSM_file:
    c = c.strip('\n')
    c = str(c)
    SMList.extend([c])


# Identify and list orphans.
for e in LGList:
    for f in SMList:
        if e == f:
            print True
            print e
            print f
            print ""
            # break
        else:
            print False
            print e
            print f
            print ""

NameLG_file.close()
NameSM_file.close()

Name2.txt contains

"teardown"
"Elite"
Binary

Inc2.txt contains

teardown
Elite

The output is:

True
teardown
teardown

False
teardown
Elite

False
Elite
teardown

False
Elite
Elite

False
Binary
teardown

False
Binary
Elite

As such I want the matching Elite strings to show True. Thanks in advance!

JSolano
  • 51
  • 1
  • 1
  • 8

1 Answers1

1

For my point of the view the problem is that your files use windows EOL (\r\n) So when you do a.strip('\n') the returned value is "Elite\r" and not "Elite"

Solution is to use strip() instead of strip('\n')

trnsnt
  • 674
  • 4
  • 15
  • Great! That worked. I am a little apprehensive though, does strip() only get rid of carriage return? – JSolano May 12 '14 at 15:34
  • It will remove all withespaces, you can find information on the official doc: [link](https://docs.python.org/2/library/stdtypes.html#str.strip) – trnsnt May 12 '14 at 16:10