LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Python - How to get text file data into an array on python. (https://www.linuxquestions.org/questions/programming-9/python-how-to-get-text-file-data-into-an-array-on-python-772544/)

golmschenk 11-30-2009 06:22 PM

Python - How to get text file data into an array on python.
 
Ok, so I've already used line split stuff to transform my data into something like this in a text file:
Code:

['1', '1', '3', '20.7505207']
['2', '1', '3', '23.0488319']
['3', '1', '3', '-1.5768747']
['4', '1', '3', '-26.4772491']
['5', '1', '3', '22.2980995']
['1', '1', '4', '73.6363678']
['2', '1', '4', '60.5475273']
['3', '1', '4', '-28.235672']
['4', '1', '4', '-8.9851713']
['5', '1', '4', '-13.4222736']
['1', '1', '5', '323.2479858']
['2', '1', '5', '-12.5010128']

How can I get this on a python program so I can manipulate it as an array?

Thanks.

jhwilliams 11-30-2009 06:52 PM

Here's /a/ way.

(1) First, get rid of the array business.
Code:

cat foo.txt | sed "s/[',[]]*//g" > bar.txt
provides:
Code:

1 1 3 20.7505207
2 1 3 23.0488319
3 1 3 -1.5768747
4 1 3 -26.4772491
5 1 3 22.2980995
1 1 4 73.6363678
2 1 4 60.5475273
3 1 4 -28.235672
4 1 4 -8.9851713
5 1 4 -13.4222736
1 1 5 323.2479858
2 1 5 -12.5010128

(2) Next, get a python list of lists:

Code:

#!/usr/bin/python

fh = open( "bar.txt" );

x = []
for line in fh.readlines():
    y = [value for value in line.split()]
    x.append( y )

fh.close()
print x

Or import array.* and use the array stuff. Or NumPy/matrices.

ghostdog74 11-30-2009 06:55 PM

you can try eval. But seriously, you should consider the pickle module if you want to store data structures in a file and reload it back.

golmschenk 06-16-2011 11:59 AM

Oh wow! I never thanked you two. I've used this information several times now. The only reason I'm here again is because I wanted to refresh my mind on what I was doing with this. So, even though it's a couple years late, thank you much!

(Um... I haven't been on LQ for a while and I don't see the "Solved" option in the thread tools anymore. Did they remove this?)

erbarker 11-11-2013 09:15 AM

I also used this information. Thanks!


All times are GMT -5. The time now is 02:37 AM.