Is there a simple, elegant way to iterate over columns in CSV files using Python?
I'm trying to process some CSV files using Python and its built-in csv
module. However, the files are all column-oriented, and csv
seems purely row-oriented. Ideally, I'd like to iterate over columns in the same way that it lets you iterate through rows.
The input file may consist of an arbitrary number of columns (probably less than 50, but there is no hard limit) and a much larger number of rows (possibly thousands), but there are varying numbers of rows in each column. I'd like to have the column as a list that I can then play with.
I feel like there has to be a simple way to do this, but I can't figure it out and I'm working on something of a deadline. Is there an easy way that I'm missing? It seems like this has to be a common issue, since non-programmer spreadsheet users tend to create column-oriented files all the time
As a workaround, I've been opening the CSV in Excel and transposing the data, making the lists into rows instead of columns, but this is time-consuming and just rubs me the wrong way anyway.
Just to clarify, when I say "column-oriented", this is what I mean:
There is no logical connection between "Alabama", "Red" and "pine". The logical grouping is a list of states, then a totally separate list of colors, and then a totally separate list of trees. I want to work on each list separately.