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


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

Re: CSV to matrix array

Started byJean-Michel Pichavant <jeanmichel@sequans.com>
First post2013-04-12 19:23 +0200
Last post2013-04-12 10:29 -0700
Articles 7 — 5 participants

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


Contents

  Re: CSV to matrix array Jean-Michel Pichavant <jeanmichel@sequans.com> - 2013-04-12 19:23 +0200
    Re: CSV to matrix array Ana Dionísio <anadionisio257@gmail.com> - 2013-04-12 10:29 -0700
      Re: CSV to matrix array Dave Angel <davea@davea.name> - 2013-04-12 13:46 -0400
      Re: CSV to matrix array Javier Miranda <javier@codecoa.com> - 2013-04-12 17:45 -0500
      Re: CSV to matrix array cantorp@gmail.com - 2013-04-13 10:06 -0700
      Re: CSV to matrix array cantorp@gmail.com - 2013-04-13 10:06 -0700
    Re: CSV to matrix array Ana Dionísio <anadionisio257@gmail.com> - 2013-04-12 10:29 -0700

#43472 — Re: CSV to matrix array

FromJean-Michel Pichavant <jeanmichel@sequans.com>
Date2013-04-12 19:23 +0200
SubjectRe: CSV to matrix array
Message-ID<mailman.530.1365787506.3114.python-list@python.org>
----- Original Message -----
> Hello!
> 
> I have a CSV file with 20 rows and 12 columns and I need to store it
> as a matrix. I already created an array with zeros, but I don't know
> how to fill it with the data from the csv file. I have this script:
> 
> import numpy
> from numpy import array
> from array import *
> import csv
> 
> input = open('Cenarios.csv','r')
> cenario = csv.reader(input)
> 
> array=numpy.zeros([20, 12])
> 
> 
> I know I have to use for loops but I don't know how to use it to put
> the data the way I want. Can you help me?
> 
> Thanks!

did you try 

array = numpy.array(list(cenario))

?

JM



-- IMPORTANT NOTICE: 

The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.

[toc] | [next] | [standalone]


#43473

FromAna Dionísio <anadionisio257@gmail.com>
Date2013-04-12 10:29 -0700
Message-ID<eed71ec6-589c-4b10-a617-a8f5145642a6@googlegroups.com>
In reply to#43472
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  

[toc] | [prev] | [next] | [standalone]


#43476

FromDave Angel <davea@davea.name>
Date2013-04-12 13:46 -0400
Message-ID<mailman.532.1365788811.3114.python-list@python.org>
In reply to#43473
On 04/12/2013 01:29 PM, Ana Dionísio 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 = csv.reader(....)
     mylist = 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, 
if you really need one of those.


-- 
DaveA

[toc] | [prev] | [next] | [standalone]


#43490

FromJavier Miranda <javier@codecoa.com>
Date2013-04-12 17:45 -0500
Message-ID<mailman.541.1365806733.3114.python-list@python.org>
In reply to#43473

[Multipart message — attachments visible in raw view] — view raw

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

>>> example_data = range(15)

>>> split_array = 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 <https://gist.github.com/anonymous/f9064e4c8790ae037ec6>

What do you guys think?


On Fri, Apr 12, 2013 at 12:46 PM, Dave Angel <davea@davea.name> wrote:

> On 04/12/2013 01:29 PM, Ana Dionísio 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 = csv.reader(....)
>     mylist = 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, if
> you really need one of those.
>
>
> --
> DaveA
> --
> http://mail.python.org/**mailman/listinfo/python-list<http://mail.python.org/mailman/listinfo/python-list>
>



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

[toc] | [prev] | [next] | [standalone]


#43526

Fromcantorp@gmail.com
Date2013-04-13 10:06 -0700
Message-ID<b414f918-3163-42fb-9c0a-5d74f3fede8f@googlegroups.com>
In reply to#43473
Dear Ana,

your example data could be transformed into a matrix with

>>>import csv
>>>rows = csv.reader(open("your_data_file.csv"), delimiter=" ")
>>>array = [row for row in rows]
>>>array[0][3]
4

HTH
Paolo

Am Freitag, 12. April 2013 19:29:05 UTC+2 schrieb Ana Dionísio:
> 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

[toc] | [prev] | [next] | [standalone]


#43527

Fromcantorp@gmail.com
Date2013-04-13 10:06 -0700
Message-ID<mailman.558.1365872784.3114.python-list@python.org>
In reply to#43473
Dear Ana,

your example data could be transformed into a matrix with

>>>import csv
>>>rows = csv.reader(open("your_data_file.csv"), delimiter=" ")
>>>array = [row for row in rows]
>>>array[0][3]
4

HTH
Paolo

Am Freitag, 12. April 2013 19:29:05 UTC+2 schrieb Ana Dionísio:
> 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

[toc] | [prev] | [next] | [standalone]


#43474

FromAna Dionísio <anadionisio257@gmail.com>
Date2013-04-12 10:29 -0700
Message-ID<mailman.531.1365787753.3114.python-list@python.org>
In reply to#43472
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  

[toc] | [prev] | [standalone]


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


csiph-web