Path: csiph.com!usenet.pasdenom.info!news.etla.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!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.005 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'example:': 0.03; 'element': 0.07; 'puts': 0.07; 'skip:` 10': 0.07; 'url:github': 0.09; 'cc:addr:python-list': 0.11; '333': 0.16; 'csv': 0.16; 'lambda': 0.16; 'mylist': 0.16; 'numpy': 0.16; 'subject:CSV': 0.16; 'subject:array': 0.16; 'suggested,': 0.16; 'think?': 0.16; 'wrote:': 0.18; 'split': 0.19; '>>>': 0.22; 'import': 0.22; 'separate': 0.22; 'cc:addr:python.org': 0.22; 'print': 0.22; '>>>': 0.24; 'skip:` 20': 0.24; 'guys': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'this:': 0.26; 'header:In- Reply-To:1': 0.27; 'array': 0.29; 'message-id:@mail.gmail.com': 0.30; 'url:mailman': 0.30; '13,': 0.31; 'file:': 0.31; 'url:python': 0.33; 'fri,': 0.33; 'position.': 0.33; 'could': 0.34; 'something': 0.35; 'convert': 0.35; 'received:google.com': 0.35; 'really': 0.36; 'url:listinfo': 0.36; 'url:org': 0.36; 'mobile:': 0.37; 'list': 0.37; 'pm,': 0.38; '12,': 0.39; 'url:mail': 0.40; 'how': 0.40; 'dave': 0.60; 'skip:\xc2 10': 0.60; 'details.': 0.61; 'pick': 0.64; 'ana': 0.84; 'miranda': 0.84; 'presumably': 0.84; 'angel': 0.91; '2013': 0.98 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=mime-version:x-received:x-originating-ip:in-reply-to:references :date:message-id:subject:from:to:cc:content-type:x-gm-message-state; bh=dcUyv7tVJnCuX1PpSjgxapwS0uAKwtH0P9LY0g7d0BI=; b=gs5ew7bN7+BXLuO66+NUQF65Chb83ZrG85B7H4coYlZN1IY23IBf7scaF7O2IvH+/L Qtry02jRbXjVubsgGzo9cj2JJY7wWSsPOxdC0Z0q1F1zLxl9g5ajdLvHRFMPTcGQDRx3 Oeouac41t0BLEA9nmZxKz5yvOtmcnmJMWM1ezKUDnc1q/JrJiDTBX3pN6FJNSotKK25J pPaaKbWHE6ai8syitRK8WLR89oj7VD2xPvS7XmdPl+OZKnYGTmCH+des0chbuYAteUQc rqMsq2JbEO1O71qmrbL91q79Q3w0rxUzkXxRDq8L6IyPuMxfGuscoKqi/kINcIDLe/Ll Pozg== MIME-Version: 1.0 X-Received: by 10.60.15.71 with SMTP id v7mr384339oec.10.1365806724137; Fri, 12 Apr 2013 15:45:24 -0700 (PDT) X-Originating-IP: [187.163.11.168] In-Reply-To: <5168486E.9000009@davea.name> References: <2506a155-40b6-4040-bc12-a08ce0d79cd7@googlegroups.com> <5168486E.9000009@davea.name> Date: Fri, 12 Apr 2013 17:45:24 -0500 Subject: Re: CSV to matrix array From: Javier Miranda To: Dave Angel Content-Type: multipart/alternative; boundary=089e013cc14a9fdc7004da31a84f X-Gm-Message-State: ALoCoQkzzap91IeBHpsA50Ey5gfcBHMU3Krge9n6aXBoPHhIfKewtn0tcaZTQT8rCI2ttfU2QETP Cc: python-list@python.org 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: 147 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1365806734 news.xs4all.nl 2642 [2001:888:2000:d::a6]:33370 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:43490 --089e013cc14a9fdc7004da31a84f Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Keep the flattened data array others suggested, and then just split it like this: *(replace `example_data`, `_array`, and `columns`)* >>> example_data =3D range(15) >>> split_array =3D lambda _array, colums: \ . . . [_array[i:i + colums] for i in \ . . . xrange(0, len(_array), colums)] >>> print split_array(example_data, 5) [[0, 1, 2, 3, 4], [5, 6, 7, 8, 9], [10, 11, 12, 13, 14]] Gist What do you guys think? On Fri, Apr 12, 2013 at 12:46 PM, Dave Angel wrote: > On 04/12/2013 01:29 PM, Ana Dion=C3=ADsio wrote: > >> That only puts the data in one column, I wanted to separate it. >> >> For example: >> data in csv file: >> >> 1 2 3 4 5 >> 7 8 9 10 11 >> a b c d e >> >> I wanted an array where I could pick an element in each position. In the >> case above if I did print array[0][3] it would pick 4 >> >> > I know nothing about numpy. > > How about something like: > import csv > myreader =3D csv.reader(....) > mylist =3D list(myreader) > > > Presumably you can fill in the details. Anyway, I think this will give > you a list of lists, and perhaps you can convert that to a numpy array, i= f > you really need one of those. > > > -- > DaveA > -- > http://mail.python.org/**mailman/listinfo/python-list > --=20 Javier Miranda Mobile: +52 333 129 20 70 --089e013cc14a9fdc7004da31a84f Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable

Keep the flattened data array others suggested,=C2=A0an= d then just split it like this: (replace `example_data`, `_array`, and `= columns`)

>>> example_data =3D range(15)

>>> split_array =3D lambda _= array, colums: \

. . . =C2=A0 =C2=A0 =C2=A0 =C2=A0[_ar= ray[i:i + colums] for i in = \

. . . =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0xrange(0, len(_array), colum= s)]

>>> print split_array(exampl= e_data, 5)

[[0, 1, 2, 3, 4], [5, 6= , 7, 8, 9], [10, 11, 12, 13, 14]]

Gist

What do you guys think?



On Fri, Apr 12, 2013 at 12:46 PM, D= ave Angel <davea@davea.name> wrote:
On 04/12/2013 01:29 PM, An= a Dion=C3=ADsio wrote:
That only puts the data in one column, I wanted to separate it.

For example:
data in csv file:

1 2 3 4 5
7 8 9 10 11
a b c d e

I wanted an array where I could pick an element in each position. In the ca= se above if I did print array[0][3] it would pick 4


I know nothing about numpy.

How about something like:
=C2=A0 =C2=A0 import csv
=C2=A0 =C2=A0 myreader =3D csv.reader(....)
=C2=A0 =C2=A0 mylist =3D list(myreader)


Presumably you can fill in the details. =C2=A0Anyway, I think this will giv= e you a list of lists, and perhaps you can convert that to a numpy array, i= f you really need one of those.


--
DaveA
--
http://mail.python.org/mailman/listinfo/python-list



--
Javier Miranda
Mobile: +52 333 129 20 70
--089e013cc14a9fdc7004da31a84f--