Python load list from file and print random selection
Question: I have a file called `projects.txt` that contains the following line of code:

    
    
    ['Apollo', 'Astro', 'Aurora', 'Barcelona']
    

I use the following Python2 code to load the file and try to print a random
selection but I'm always getting just a single character from my list:

    
    
    import random
    
    projNames = open('projects.txt').read()
    print(random.choice(projNames))
    

**Example Result:** a

* * *

However, if I copy the contents of the text file directly into the code, it
works properly for me.

    
    
    import random
    
    projNames = ['Apollo', 'Astro', 'Aurora', 'Barcelona']
    print(random.choice(projNames))
    

**Example Result:** Apollo

I need it to be able to load the contents of the file directly rather than
editing the code in my `.py` file.

What did I overlook in my original code? I am specifically using Python 2.7.x
_(latest version)_.


Answer: You have to parse\eval string first to make it Python list:

    
    
    import ast
    import random
    
    projNames = ast.literal_eval(open('projects.txt').read())
    print(random.choice(projNames))
    

