Friday, August 5, 2016

Data File Entries into Empty Lists

Hello All,

I have what I hope will be a simple question.
I have a text file that has a list of sample names, one per line as below:

STX2
XXX3
DXM4
etc....

I can read the file into a list:

file = open(str(filepath) + '/SampleNames.txt')
lines = file.readlines()

querylist = []
linenumber = 0

for i in lines:
    linenumber = linenumber + 1
    querylist.append(lines[linenumber - 1])

   

So I now have a list : querylist['STX2n\', 'XXX3n\', 'DXM4n\', etc....]

I need to be able to convert each item in the list into a new, standalone, empty list. Something that would appear like below once finalized:

STX2 = []
XXX3 = []
DXM4 = []

Try as I might, I've been unable to come up with a simple solution.  There is surely a function or command I don't know about that will accomplish this, as it seems pretty straight forward...

Thanks in advance for any help,

Josh Peek

**********************************************************************************
EDIT:  8/12/16

I've found what appears to be a simple solution, seen below:

for item in querylist:
   exec((item) + (' = []'))


The function exec() executes string input as python code.  So as of now, I am pulling each item in the querylist[] into the exec() function as a string and executing it.  This builds each list as I wanted.  I don't know if this is proper or not, but it seems to be working well...










Monday, August 1, 2016