Simple random name generator in Python
Question: I have a text file containing first and last 'syllables', demarcated with
[part1] and [part2]:

    
    
    [part1]
    Ae
    Di
    Mo
    Fam
    [part2]
    dar
    kil
    glar
    tres
    

All I want to do is pick a random line, between [part1] and [part2], and then
another random line between [part2] and the end of the file, and concatenate
the two together (e.g. "Aedar", "Moglar") to create random names.

However I am unsure how to parse the text file effectively with readline(). Is
there a better way than scanning through each line sequentially, and storing
all of them in a list from whence I can pick a random element?


Answer: Serialize ([pickle](http://docs.python.org/library/pickle.html)) a dictionary
to a file instead.

Example:

    
    
    # create the dict and save it to a file
    d={
    'part1':[
        'Ae',
        'Di',
        'Mo',
        'Fam',],
    'part2':[
        'dar',
        'kil',
        'glar',
        'tres',],
    }
    
    import pickle
    f=open('syllables','w')
    pickle.dump(d,f)
    f.close()
    
    
    # read the dict back in from the file
    f1=open('syllables','r')
    sd=pickle.load(f1)
    f1.close()
    
    import random
    first_part=sd['part1'][random.randint(0,len(sd['part1'])-1)]
    second_part=sd['part2'][random.randint(0,len(sd['part2'])-1)]
    
    print '%s%s'%(first_part,second_part)
    

