0

I’m new to python and am trying to puzzle out the use of the lambda function. I have two lists of network user names in a text file which I need to match. The code I have so far works okay(it matches names but is case sensitive), but the text in both these files is a mishmash of upper and lower case. I may have smith, john (FINANCE) in one list and SMITH,John (Finance) in another. There will be hundreds of user text files.What I need to do is normalize both lists (to upper case for example) so matches occur regardless of case. My lack of python knowledge is hampering me. I have the following

with open (filename, "r") as file1:
    #file1m=map(lambda x: x.upper(),file1)
    for line in islice(file1,20,None)
        with open ("c:\\userlists\test.txt", "r") as file2:

But, to be honest I don't know where the lambda function sits in that bit of code. I've tried it where you see the hash, but python never seems to make a username match. I know I need to do upper case file2, but for this test, and to simplify the process for me, I've added a few names in upper case in test.txt to see if it works. Without the lambda function, as mentioned my code does what I need and matches username but is case sensitive. Any help would be really appreciated.

Many thanks

2
  • uncomment your second line, then use file1m instead of file1 afterward Commented May 30, 2013 at 12:39
  • Thanks. I've tried that and using file1m (so the upper case reading of file1) python doesn't seem to make a match. IDLE just sits there and I have to break out using the keyboard. Commented May 30, 2013 at 12:49

2 Answers 2

1

You could create a simple Context Manager to allow the files to be converted to uppercase transparently as they're read. Here's an example of what I'm suggesting:

from itertools import imap

class OpenUpperCase(object):
    def __init__(self, *args, **kwargs):
        self.file = open(*args, **kwargs)
    def __enter__(self):
        return imap(lambda s: s.upper(), self.file)
    def __exit__( self, type, value, tb ):
        self.file.close()
        return False  # allow any exceptions to be processed normally

if __name__ == '__main__':
    from itertools import islice

    filename1 = 'file1.txt'
    with OpenUpperCase(filename1, "r") as file1:
        for line in islice(file1, 20, None):
            print line,  # will be uppercased
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks @martineau. That really makes sense and is a excellent answer. I can see both my lists being printed in upper-case. Now, I’m hoping I don’t want the moon on a stick here, but I can’t seem to get a match. I’m using for i in file1, for j in file2 to hopefully get an i==j. Is that way off what I need to do? Apologies as a S.O newb and Python newb, I don't know how best to return a following question.
Many thanks @martineau. I've worked out where the code was failing with your help. Best wishes
1

You can use this to convert your files to uppercase. Then you can do what you want with them. Or you can just use the code below as an idea, and adapt it to work with what you are trying to do.

file1 = "C:/temp/file1.txt" # first file
file2 = "C:/temp/file2.txt"  # second file

m_upper = lambda x: x.upper() # uppercase lambda function.

# open the files, read them, map using the mapper,
# and write them back with append.
def map_file(file_path, append='_upper', mapper=m_upper):
    file_name, ext = file_path.rsplit('.', 1)
    new_fp = '%s%s.%s' % (file_name, append, ext)
    with open(file_path) as f_in, open(new_fp, 'w') as f_out:
        f_out.write(''.join(map(mapper, f_in)))

map_file(file1) # file1.txt -> file1_upper.txt (all uppercase)
map_file(file2) # file2.txt -> file2_upper.txt (all uppercase)

1 Comment

Thanks for your help. I'll give it a try and report back if I need to.

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.