?
Solved

loadtxt using numpy in python

Posted on 2011-09-28
1
Medium Priority
?
884 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 2

Accepted Solution

by:
wmatos earned 2000 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

The purpose of this article is to demonstrate how we can use conditional statements using Python.
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
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 …
Suggested Courses

777 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