You should put the entries in a dictionary, not in so many separate variables -- clearly, the keys you're using need NOT be acceptable as variable names (that slash in 'Url/Host' would be a killer!-), but they'll be just fine as string keys into a dictionary.
import re
there = re.compile(r'''(?x) # verbose flag: allows comments & whitespace
^ # anchor to the start
([^:]+) # group with 1+ non-colons, the key
:\s* # colon, then arbitrary whitespace
(.*) # group everything that follows
$ # anchor to the end
''')
and then
configdict = {}
for aline in open('thefile.txt'):
mo = there.match(aline)
if not mo:
print("Skipping invalid line %r" % aline)
continue
k, v = mo.groups()
configdict[k] = v
the possibility of making RE patterns "verbose" (by starting them with (?x)
or using re.VERBOSE
as the second argument to re.compile
) is very useful to allow you to clarify your REs with comments and nicely-aligning whitespace. I think it's sadly underused;-).