Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python csv text file to array[i,j]

I just started to learn python, so I need some help.

I have closeparams.txt file, it has CSV structure:

3;700;3;10;1
6;300;3;20;1
9;500;2;10;5

I need read this file to 2 dimension array. a[i,j] where i - is row and j - is column

I searched but not found exactly samples. I will use this massive like this:

i=0
j=3
print a(i,j)

I suppose that display:

10

Or

i=2
j=1
print a(i,j)

I suppose that display:

500
like image 668
RedSubmarine Avatar asked Nov 14 '12 15:11

RedSubmarine


1 Answers

I suggest to use numpy if you want to deal with arrays. In your case:

import numpy

a = numpy.loadtxt('apaga.txt', delimiter=';')

print a[0,3]

You didn't specify how important will the array construct be for you, but Numpy is very, very powerful for complex tasks, and can be very lean to perform smaller, quick'n'dirty tasks in a compact, fast and readable way.

like image 57
heltonbiker Avatar answered Sep 23 '22 00:09

heltonbiker