Solved

loadtxt using numpy in python

Posted on 2011-09-28
1
819 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show the steps for installing Python on Ubuntu Operating System. I have created a virtual machine with Ubuntu Operating system 8.10 and this installing process also works with upgraded version of Ubuntu OS. For installing Py…
Article by: Swadhin
Introduction of Lists in Python: There are six built-in types of sequences. Lists and tuples are the most common one. In this article we will see how to use Lists in python and how we can utilize it while doing our own program. In general we can al…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
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…

914 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

16 Experts available now in Live!

Get 1:1 Help Now