LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-30-2009, 06:22 PM   #1
golmschenk
Member
 
Registered: Nov 2009
Posts: 144

Rep: Reputation: 15
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.
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 11-30-2009, 06:52 PM   #2
jhwilliams
Senior Member
 
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,168

Rep: Reputation: 211Reputation: 211Reputation: 211
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.

Last edited by jhwilliams; 11-30-2009 at 06:54 PM.
 
2 members found this post helpful.
Old 11-30-2009, 06:55 PM   #3
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
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.
 
Old 06-16-2011, 11:59 AM   #4
golmschenk
Member
 
Registered: Nov 2009
Posts: 144

Original Poster
Rep: Reputation: 15
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?)

Last edited by golmschenk; 06-16-2011 at 12:01 PM.
 
Old 11-11-2013, 09:15 AM   #5
erbarker
LQ Newbie
 
Registered: Dec 2008
Posts: 2

Rep: Reputation: 0
I also used this information. Thanks!
 
  


Reply

Tags
python


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Split header from data in file using python pwc101 Programming 13 06-16-2009 07:27 PM
Python Reading and Declaring Variables from a Text File dudeman41465 Programming 7 01-31-2009 04:54 PM
Python: how to import a python file that doesn't end in .py? BrianK Programming 5 04-15-2008 12:35 PM
Python: Can you add text to an image in python? BrianK Programming 2 04-03-2008 03:56 PM
Python: find defined text string in a file, and replace the whole line Dark Carnival Programming 6 05-22-2007 06:02 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:00 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration