How do I search a string for anything?

What I mean is, I need to say:

if line == 'playername = (any name)':
    temp = line.strip().replace('playername=', '')
return temp

Any ideas?

You could use regular expressions... here's an example:

>>> import re
>>> pattern = 'playername = (.*)'
>>> rc = re.compile(pattern)
>>> for line in ['playername = Johnny', 'playername = Foo Bar the Clown', 'somet
hing other than playername', 'another dud', 'playername = Chocolate Rain']:
...   rcm = rc.match(line)
...   if rcm:
...     print (rcm.group(1))
...
Johnny
Foo Bar the Clown
Chocolate Rain
>>>

Here's the documentation on the Python re module. It's apparently a "weak" representation of regular expressions, but it's always proved good enough for me to use. I suggest you become familiar with them, as they can be very powerful for searching and/or replacing text among other things...

This is my code:

def load_ini():
    fout = open('tpp.ini', 'r')
    fout.seek(0)
    lines = fout.readlines()
    p_map = re.compile('map =  (.*)')
    p_collision = re.compile('collision = (.*)')
    p_pname  = re.compile('playername = (.*)')
    for index, line in lines:
        if line == '\n':
            continue
        elif line == '[END_OF_FILE]':
            break
        else:
            if p_map.match(line):
                temp = line.strip().replace('map=', '')
            elif p_collision.match(line):
                temp2 = line.strip().replace('collision=', '')
            elif p_pname.match(line):
                temp3 = line.strip().replace('playername=', '')
            else:
                print 'File corrupted!'
                temp, temp2, temp3 = None
                break
                
    return [temp, temp2, temp3]

And I have been getting this error for a while:

Traceback (most recent call last):
  File "\\DIVINE-HERESY\Shared\Tom's Personal Project\main.py", line 156, in <module>
    if __name__ == '__main__': main()
  File "\\DIVINE-HERESY\Shared\Tom's Personal Project\main.py", line 110, in main
    p = Player(2, "rifleman_blu", 100)
  File "\\DIVINE-HERESY\Shared\Tom's Personal Project\main.py", line 63, in __init__
    self.name = load_ini()[2]
  File "\\DIVINE-HERESY\Shared\Tom's Personal Project\main.py", line 20, in load_ini
    for index, line in lines:
ValueError: too many values to unpack

I have done this sort of thing before, but I have never got this error :S

This is the contents of the tpp.ini file:

map = test.png
collision = test_collision.png
playername = defaultplayer
[END_OF_FILE]
for index, line in lines:
for index, line in lines:
ValueError: too many values to unpack

I have done this sort of thing before, but I have never got this error :S

That's because you probably used enumerate before. Enumerate gives you index, value pairs. As it stands you're only iterating over the values of the lines list; however you're trying to unpack each value into both index and line , which is where the error stems from.

Thank you, that got rid of the error, but my file parser is wrong so it always returns corrupted.

Looking back at your code....

Since you're simply using the string.replace() method I think using a regular expression is probably overkill. All you really need to do is use string.find(search_term) and check the return. This function will return the index of the starting position of search_term if found. Otherwise it will return -1. So to modify your code:

def load_ini():
    fout = open('tpp.ini', 'r')
    fout.seek(0)
    lines = fout.readlines()
    fout.close()            # I added this
    for index, line in enumerate(lines):
        if line == '\n':
            continue
        elif line == '[END_OF_FILE]':
            break
        else:
            if line.find('map =') != -1:
                temp = line.strip().replace('map =', '')
            elif line.find('collision =') != -1:
                temp2 = line.strip().replace('collision =', '')
            elif line.find('playername =') != -1:
                temp3 = line.strip().replace('playername =', '')
            else:
                print 'File corrupted!'
                temp, temp2, temp3 = None
                break
                
    return [temp, temp2, temp3]

I noticed that your input file has a space between the word (map, playername, etc) and the '=', but the replace you were using did not. This will make a difference. Especially when using the find/replace functions from string. A regular expression is more forgiving if you use the ? qualifier, which matches 0 or 1 times. The new pattern would look like this: 'collision ?= ?(.*)' ... I think. Like I said I'm not a master at regex but I do know it's powerful if you learn to use it correctly.

This question has already been answered. Start a new discussion instead.