Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'none:': 0.07; 'python3': 0.07; 'table.': 0.07; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'rows': 0.09; 'python': 0.11; 'def': 0.12; '[none]': 0.16; 'csv': 0.16; 'nan': 0.16; 'numpy': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'subject:CSV': 0.16; 'modification': 0.16; 'wrote:': 0.18; 'skip:p 40': 0.19; 'feb': 0.22; '>>>': 0.22; 'import': 0.22; 'header:User-Agent:1': 0.23; 'error': 0.23; 'header:X-Complaints- To:1': 0.27; 'skip:p 30': 0.29; 'array': 0.29; 'linux': 0.33; 'subject:with': 0.35; 'something': 0.35; 'version': 0.36; 'yield': 0.36; 'to:addr:python-list': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'received:org': 0.40; 'length': 0.61; 'mentioned': 0.61; 'maximum': 0.63; 'more': 0.64; 'trial': 0.83; '2014,': 0.84; 'subject:Load': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: Load a CSV with different row lengths Date: Wed, 30 Jul 2014 10:16:39 +0200 Organization: None References: Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p57bdbe71.dip0.t-ipconnect.de User-Agent: KNode/4.11.5 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 50 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1406708223 news.xs4all.nl 2908 [2001:888:2000:d::a6]:43352 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:75362 Miki Tebeka wrote: > Greetings, > >> I should've mentioned that I want to import my csv as a data frame or >> numpy array or as a table. > If you know the max length of a row, then you can do something like: > def gen_rows(stream, max_length): > for row in csv.reader(stream): > yield row + ([None] * (max_length - len(line)) > > max_length = 10 > with open('data.csv') as fo: > df = pd.DataFrame.from_records(gen_rows(fo, max_length)) With the help of the search engine that must not be named and some trial and error I also found a way to use pandas.read_csv(): $ cat data.csv a,b a,b,c,d a,b,c $ python3 Python 3.3.2+ (default, Feb 28 2014, 00:52:16) [GCC 4.8.1] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import pandas >>> pandas.read_csv("data.csv", names=list(range(4))) 0 1 2 3 0 a b NaN NaN 1 a b c d 2 a b c NaN And if the maximum row length is not known here's a modification of Miki's recipe: def gen_rows(stream, max_length=None): rows = csv.reader(stream) if max_length is None: rows = list(rows) max_length = max(len(row) for row in rows) for row in rows: yield row + [None] * (max_length - len(row)) with open('data.csv') as f: df = pd.DataFrame.from_records(list(gen_rows(f))) # my version of pandas # does not accept a # generator