Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #75342 > unrolled thread

Re: Load a CSV with different row lengths

Started byPeter Otten <__peter__@web.de>
First post2014-07-29 09:37 +0200
Last post2014-07-29 09:37 +0200
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Load a CSV with different row lengths Peter Otten <__peter__@web.de> - 2014-07-29 09:37 +0200

#75342 — Re: Load a CSV with different row lengths

FromPeter Otten <__peter__@web.de>
Date2014-07-29 09:37 +0200
SubjectRe: Load a CSV with different row lengths
Message-ID<mailman.12409.1406619473.18130.python-list@python.org>
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).

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web