Solved

loadtxt using numpy in python

Posted on 2011-09-28
1
806 Views
Last Modified: 2012-05-12
Hi

I have data files that has blocks of data (see the file attached). I need to read only the first block of data in to an array (index 0) using python, at this point i load all the data. I can do this by checking the value of idex in the array data after I load them, but I was wondering if there is any easy way to do this.

I use 'loadtxt' in numpy to load the data.


thanks in advance
~Prabath




admconstraints--hamiltonian.x.txt
0
Comment
Question by:Peiris
1 Comment
 
LVL 2

Accepted Solution

by:
wmatos earned 500 total points
ID: 36814435
In the simplest of cases, a call like:
 
  mylist = [line.split(DELIMITER)[0] for line in open(FILENAME,'r')]

However, I noticed that your file has some other data you'll want to exclude.  Therefore, you'll have to be a little more selective by adding an if at the end... like:

  mylist = [line.split(DELIMITER)[0] for line in open(FILENAME, 'r') if not line.startswith('#')]
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction On September 29, 2012, the Python 3.3.0 was released; nothing extremely unexpected,  yet another, better version of Python. But, if you work in Microsoft Windows, you should notice that the Python Launcher for Windows was introduced wi…
Strings in Python are the set of characters that, once defined, cannot be changed by any other method like replace. Even if we use the replace method it still does not modify the original string that we use, but just copies the string and then modif…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now