Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7934581
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T21:41:05+00:00 2026-06-03T21:41:05+00:00

I’m new to python and am trying to read blocks of data from a

  • 0

I’m new to python and am trying to read “blocks” of data from a file. The file is written something like:

# Some comment
# 4 cols of data --x,vx,vy,vz
# nsp, nskip =           2          10


#            0   0.0000000


#            1           4
 0.5056E+03  0.8687E-03 -0.1202E-02  0.4652E-02
 0.3776E+03  0.8687E-03  0.1975E-04  0.9741E-03
 0.2496E+03  0.8687E-03  0.7894E-04  0.8334E-03
 0.1216E+03  0.8687E-03  0.1439E-03  0.6816E-03


#            2           4
 0.5056E+03  0.8687E-03 -0.1202E-02  0.4652E-02
 0.3776E+03  0.8687E-03  0.1975E-04  0.9741E-03
 0.2496E+03  0.8687E-03  0.7894E-04  0.8334E-03
 0.1216E+03  0.8687E-03  0.1439E-03  0.6816E-03


#          500  0.99999422


#            1           4
 0.5057E+03  0.7392E-03 -0.6891E-03  0.4700E-02
 0.3777E+03  0.9129E-03  0.2653E-04  0.9641E-03
 0.2497E+03  0.9131E-03  0.7970E-04  0.8173E-03
 0.1217E+03  0.9131E-03  0.1378E-03  0.6586E-03

and so on

Now I want to be able specify and read only one block of data out of these many blocks. I’m using numpy.loadtxt('filename',comments='#') to read the data but it loads the whole file in one go. I searched online and someone has created a patch for the numpy io routine to specify reading blocks but it’s not in mainstream numpy.

It’s much easier to choose blocks of data in gnuplot but I’d have to write the routine to plot the distribution functions. If I can figure out reading specific blocks, it would be much easier in python. Also, I’m moving all my visualization codes to python from IDL and gnuplot, so it’ll be nice to have everything in python instead of having things scattered around in multiple packages.

I thought about calling gnuplot from within python, plotting a block to a table and assigning the output to some array in python. But I’m still starting and I could not figure out the syntax to do it.

Any ideas, pointers to solve this problem would be of great help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T21:41:07+00:00Added an answer on June 3, 2026 at 9:41 pm

    A quick basic read:

    >>> def read_blocks(input_file, i, j):
        empty_lines = 0
        blocks = []
        for line in open(input_file):
            # Check for empty/commented lines
            if not line or line.startswith('#'):
                # If 1st one: new block
                if empty_lines == 0:
                    blocks.append([])
                empty_lines += 1
            # Non empty line: add line in current(last) block
            else:
                empty_lines = 0
                blocks[-1].append(line)
        return blocks[i:j + 1]
    
    >>> for block in read_blocks(s, 1, 2):
        print '-> block'
        for line in block:
            print line
    
    
    -> block
     0.5056E+03  0.8687E-03 -0.1202E-02  0.4652E-02
     0.3776E+03  0.8687E-03  0.1975E-04  0.9741E-03
     0.2496E+03  0.8687E-03  0.7894E-04  0.8334E-03
     0.1216E+03  0.8687E-03  0.1439E-03  0.6816E-03
    -> block
     0.5057E+03  0.7392E-03 -0.6891E-03  0.4700E-02
     0.3777E+03  0.9129E-03  0.2653E-04  0.9641E-03
     0.2497E+03  0.9131E-03  0.7970E-04  0.8173E-03
     0.1217E+03  0.9131E-03  0.1378E-03  0.6586E-03
    >>> 
    

    Now I guess you can use numpy to read the lines…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to render a haml file in a javascript response like so:
I have some data like this: 1 2 3 4 5 9 2 6
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.