-2

I would like to know can we read line by line in an array. For example:

array([[ 0.28,  0.22,  0.23,  0.27],
       [ 0.12,  0.29,  0.34,  0.21],
       [ 0.44,  0.56,  0.51,  0.65]])

To read the first line as in array form to perform some operation and then continue with second row array:

array([0.28,0.22,0.23,0.27])

What produces the above array is because of these two lines of code:

from numpy import genfromtxt
single=genfromtxt('single.csv',delimiter=',')

single.csv

0.28,  0.22,  0.23,  0.27
0.12,  0.29,  0.34,  0.21
0.44,  0.56,  0.51,  0.65

Using readlines() seem like producing list instead of array. In my case, I'm using csv file. I'm trying to use the rows of values line by line instead of using them all together to avoid memory error. Can anyone help me?

with open('single.csv') as single:
    single=single.readlines()
2
  • Does your csv have array([0.28,0.22,0.23,0.27]) in it? That isn't csv format. Commented Feb 23, 2016 at 13:16
  • readlines produces a list of strings. Parse each line to get the numbers Commented Feb 23, 2016 at 13:58

3 Answers 3

10

you can use np.fromstring

import numpy as np
with open('single.csv') as f:
    lines=f.readlines()
    for line in lines:
        myarray = np.fromstring(line, dtype=float, sep=',')
        print(myarray)

see http://docs.scipy.org/doc/numpy-1.10.1/reference/generated/numpy.fromstring.html and How to read csv into record array in numpy?

Sign up to request clarification or add additional context in comments.

1 Comment

Sorry I'm not familiar with np.fromstring. And it returned this error: NameError: name 'double' is not defined
8

Seems you don't have experience reading files in Python. Let me work through an example in some detail, in an Ipython iteractive session

Create a multiline text to simulate your file

In [23]: txt="""0.28,  0.22,  0.23,  0.27
0.12,  0.29,  0.34,  0.21
0.44,  0.56,  0.51,  0.65"""

split it into lines to simulate the result of readlines

In [24]: txt=txt.splitlines(True)

In [25]: txt
Out[25]: 
['0.28,  0.22,  0.23,  0.27\n',
 '0.12,  0.29,  0.34,  0.21\n',
 '0.44,  0.56,  0.51,  0.65']

I can transform it into an array with genfromtxt (you could have passed the result to readlines to genfromtxt like this.

In [26]: np.genfromtxt(txt, delimiter=',')
Out[26]: 
array([[ 0.28,  0.22,  0.23,  0.27],
       [ 0.12,  0.29,  0.34,  0.21],
       [ 0.44,  0.56,  0.51,  0.65]])

I can iterate over the lines, strip off the \n and split on ','

In [27]: for line in txt:
    print line.strip().split(',')
   ....:     
['0.28', '  0.22', '  0.23', '  0.27']
['0.12', '  0.29', '  0.34', '  0.21']
['0.44', '  0.56', '  0.51', '  0.65']

I can transform each string into a float with a list comprehension:

In [28]: for line in txt:                                  
    print [float(x) for x in line.strip().split(',')]
   ....:     
[0.28, 0.22, 0.23, 0.27]
[0.12, 0.29, 0.34, 0.21]
[0.44, 0.56, 0.51, 0.65]

Or by putting the iteration in another list comprehension, I can get a list of lists of numbers:

In [29]: data=[[float(x) for x in line.strip().split(',')] for line in  txt]

In [30]: data
Out[30]: [[0.28, 0.22, 0.23, 0.27], [0.12, 0.29, 0.34, 0.21], [0.44, 0.56, 0.51, 0.65]]

I can turn that into an array

In [31]: np.array(data)
Out[31]: 
array([[ 0.28,  0.22,  0.23,  0.27],
       [ 0.12,  0.29,  0.34,  0.21],
       [ 0.44,  0.56,  0.51,  0.65]])

genfromtxt is essentially going through that sequence - reading the lines,spliting them, converting strings to values, and finally creating an array from the list.

There are short cuts, but I think you would benefit from working through these steps in detail. It's as much an exercise in basic Python string and list manipulation as it is about arrays.

1 Comment

Thanks :) very helpful.
0
for list in array:
    print(list)
    for item in list:
        print(item)

gives the output:

[0.28, 0.22, 0.23, 0.27]
0.28
0.22
0.23
0.27
[0.12, 0.29, 0.34, 0.21]
0.12
0.29
0.34
0.21
[0.44, 0.56, 0.51, 0.65]
0.44
0.56
0.51
0.65

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.