Pandas is in fact the right solution here. The issue is that in order to robustly handle something where you aren't certain of the underlying structure there's a lot of edge cases you have to watch out for, and trying to shoe-horn it into the csv
module is a recipe for headaches (though it can be done)
As far as why you can't import pandas
the reason is that it doesn't come with python
by default. One of the most important things to consider when picking up a language is the ecosystem of packages it gives you access to. Python happens to be one of the best in the respect, so to ignore everything that's not a part of standard python is to ignore the best part of the language.
If you're on a windows environment you should start by getting conda
set up. This will allow you to seamlessly explore many of the packages available to python users with little overhead. This includes pandas
, which is in fact the right way to handle this problem. See this link for more info on installing conda: http://conda.pydata.org/docs/install/quick.html
Once you're got pandas
installed it's as easy as this:
import pandas
test = pandas.read_csv(<your_file>)
your_Variable = test[<column_header>]
Easy as that.
If you really, really don't want to use things that aren't in core python then you can do this with something like what follows, but you haven't given enough detail for an actual solution:
def col_var(input_file, delimiter):
# get each line into a variable
rows = open(input_file).read().splitlines()
# split each row into entries
split_rows = [row.split(delimiter) for row in rows]
# Re-orient your list
columns = zip(*split_rows)
The least intuitive piece of this is the last line, so here's a little example showing you how it works:
>>> test = [[1,2], [3,4]]
>>> zip(*test)
[(1, 3), (2, 4)]