Load a CSV with different row lengths
Peter Otten
__peter__ at web.de
Tue Jul 29 03:37:33 EDT 2014
Ryan de Vera wrote:
> I am trying to load a csv with different row lengths. For example,
>
> I have the csv
>
> a,b
> a,b,c,d
> a,b,c
>
> How can I load this into python? I tried using both NumPy and Pandas.
The csv module in the standard library can deal with varying row lengths
just fine:
>>> import csv
>>> with open("data.csv") as f:
... data = list(csv.reader(f))
...
>>> data
[['a', 'b'], ['a', 'b', 'c', 'd'], ['a', 'b', 'c']]
Depending on your use case you may need to do some post-processing in your
code (e. g. converting strings to numbers).
More information about the Python-list
mailing list