How can I organize case-insensitive text and the material following it?
<p>I'm very new to Python so it'd  be very appreciated if this could be explained as in-depth as possible.</p>

<p>If I have some text like this on a text file:</p>

<pre><code>matthew : 60 kg
MaTtHew : 5 feet
mAttheW : 20 years old
maTThEw : student
MaTTHEW : dog owner
</code></pre>

<p>How can I make a piece of code that can write something like...</p>

<pre><code>Matthew : 60 kg , 5 feet , 20 years old , student , dog owner
</code></pre>

<p>...by only gathering information from the text file?</p>

<pre><code>import sys

# There's a number of assumptions I have to make based on your description. 
# I'll try to point those out. 

# Should be self-explanatory. something like: "C:\Users\yourname\yourfile" 
path_to_file = "put_your_path_here"

# open a file for reading. The 'r' indicates read-only 
infile = open(path_to_file, 'r')

# reads in the file line by line and strips the "invisible" endline character
readLines = [line.strip() for line in infile]

# make sure we close the file
infile.close()

# An Associative array. Does not use normal numerical indexing. 
# instead, in our case, we'll use a string(the name) to index into. 
# At a given name index(AKA key) we'll save the attributes about that person. 
names = dict()

# iterate through each line we read in from the file
# each line in this loop will be stored in the variable
# item for that iteration. 
for item in readLines:

    #assuming that your file has a strict format: 
    # name : attribute
    index = item.find(':')

    # if there was a ':' found then continue
    if index is not -1:
        # grab only the name of the person and convert the string to all lowercase
        name = item[0:index].lower()
        # see if our associative array already has that peson
        if names.has_key(name): 
            # if that person has already been indexed add the new attribute
            # this assumes there are no dupilcates so I don't check for them.
            names[name].append(item[index+1:len(item)])
        else:
            # if that person was not in the array then add them. 
            # we're adding a list at that index to store their attributes.
            names[name] = list()
            # append the attribute to the list. 
            # the len() function tells us how long the string 'item' is
            # offsetting the index by 1 so we don't capture the ':'
            names[name].append(item[index+1:len(item)])
    else: 
        # there was no ':' found in the line so skip it
        pass

# iterate through keys (names) we found. 
for name in names:
    # write it to stdout. I am using this because the "print" built-in to python
    # always ends with a new line. This way I can print the name and then 
    # iterate through the attributes associated with them
    sys.stdout.write(name + " : ")
    # iterate through attributes
    for attribute in names[name]:
        sys.stdout.write(attribute + ", ")

    # end each person with a new line.
    sys.stdout.write('\r\n')
</code></pre>

python