0
#define reader and process header
csvReader = csv.reader(tmpfile)
header = next(csvReader)
template_name_index = header.index('TemplateName')

I want the program to parse a file and look for a header 'TemplateName' but I want it to be able to find the header even if its uppercase or lowercase.

Amir
  • 10,600
  • 9
  • 48
  • 75
  • 4
    Possible duplicate of [How to convert string to lowercase in Python?](http://stackoverflow.com/questions/6797984/how-to-convert-string-to-lowercase-in-python) – Erica Jan 06 '16 at 15:21
  • I want it to be able to find this value regardless of it being uppercase, lowercase or camelcase without changing it. – Matthew McMillian Jan 06 '16 at 15:23
  • yes, but if you convert the string you are searching through to lowercase, you can the just search for 'templatename'. E.g. "RanDomTeMplAteNamE".lower() becomes "randomtemplatename" – Chris Jan 06 '16 at 15:37
  • @MatthewMcMillian `lower` will not change your string - strings are immutable in Python. – erip Jan 06 '16 at 16:16
  • @MatthewMcMillian This can be seen if you run this in the intepreter: `s = "Hello, World"; s.lower() == "hello, world"; print(s)` – erip Jan 06 '16 at 16:19
  • Possible duplicate of [What is the best way to get the first item from an iterable matching a condition?](http://stackoverflow.com/questions/2361426/what-is-the-best-way-to-get-the-first-item-from-an-iterable-matching-a-condition) – Cristian Ciupitu Jan 06 '16 at 17:30

1 Answers1

1

Since you're looking for a string in an array of strings, you might have to loop over each string. For example, this would convert the string to lower-case before making the comparison:

indexes = [index for index, name in enumerate(header) if name.lower() == "templatename"]
if len(indexes) == 1:
    index = indexes[0]            
    # There is at least one header matching "TemplateName"
    # and index now points to the first header.

Note that the if statement considers there might be no header or more than one header matching the given name. For your reassurance, also note that lower() does not change the case of the original string.

You might also find it more obvious to convert all the strings in the header to lower-case before calling index, which looks more like your original code:

try:
    index = [name.lower() for name in header].index("templatename")
except ValueError:
    # There is no header matching "TemplateName"
    # and you can use `pass` to just ignore the error.
else:
    # There is at least one header matching "TemplateName"
    # and index now points to the first header.

Note that, like before, lower() does not change the case of the actual header because it is only done within the context of the loop. In fact, strings in Python are immutable so you can't change them in place.

You might also consider regular expressions. For example, this would search for case insensitve without converting the string to lower-case:

import re
indexes = [index for index, name in enumerate(header) if re.match(name, "TemplateName", re.I)]

Note that if you don't really need the index, then you can remove enumerate and simplify the loop a little.

cr3
  • 461
  • 3
  • 8
  • I edited my code samples to be more verbose, let me know if you're still not following. If there's something specific you don't understand, please ask. – cr3 Jan 06 '16 at 15:32
  • Thank you very much I really appreciate the help. Im going to try a couple things now and Ill let you know. – Matthew McMillian Jan 06 '16 at 15:37
  • Traceback (most recent call last): File "C:\Users\502434606\Desktop\Python Projects\FileManipulator\TemplateDelete1.py", line 25, in template_name_index = header.index('TemplateName') ValueError: 'TemplateName' is not in list – Matthew McMillian Jan 06 '16 at 15:47
  • I edited my code samples again to consider the case where there might be no header matching "TemplateName" when using the `index` method. In short, you need to catch the `ValueError` exception as you can see in my middle code sample. – cr3 Jan 06 '16 at 15:53