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 8621147
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T06:45:15+00:00 2026-06-12T06:45:15+00:00

I wasted most of my morning failing to solve this simple problem. Using python,

  • 0

I wasted most of my morning failing to solve this simple problem. Using python, I want to parse data files that look like this:

# This is an example comment line, it starts with a '#' character.
# There can be a variable number of comments between each data set.
# Comments "go with" the data set that comes after them.
# The first data set starts on the next line:
0.0 1.0
1.0 2.0
2.0 3.0
3.0 4.0

# Data sets are followed by variable amounts of white space.
# The second data set starts after this comment
5.0 6.0
6.0 7.0


# One more data set.
7.0 8.0
8.0 9.0

The python code I want would parse the above example into the three “blocks”, storing them as elements of a list. The individual code-blocks could themselves be stored as lists of lines, with or without the comment lines, whatever. A handraulic way is to do this:

#! /usr/bin/env python

# Read in data, seperate into rows_alldata
f=open("example")
rows = f.read().split('\n')
f.close()

# Do you haz teh codez?
datasets=[]
datasets.append(rows[0:8])
datasets.append(rows[9:13])
datasets.append(rows[15:18])

I’m looking for a more general solution that supports variable numbers and lengths of data sets. I have tried several catastrophes built off non-pythonic looking loops. I think it best not to clutter up my question with them; this is work and not “homework”.

  • 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-12T06:45:16+00:00Added an answer on June 12, 2026 at 6:45 am

    Use groupby.

    from itertools import groupby
    
    def contains_data(ln):
        # just an example; there are smarter ways to do this
        return ln[0] not in "#\n"
    
    with open("example") as f:
        datasets = [[ln.split() for ln in group]
                    for has_data, group in groupby(f, contains_data)
                    if has_data]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'll begin by admitting that my problem is most likely the result of bad
I'm embarrassed by the 'basicness' of this question, but after wasted hours, here goes.
Most of my C++ programming experience has been projects for school. In that way,
OK so having a real headache with this one and most of the day
In my script I have four functions that work like this: def function_four(): #
This question if a bit off topic, I just wanted feedback from most ActionScript/Flex
This may be the most foolish question. But I just wanted to hear from
I'm working with lots of log files, and most log files have lots of
I'd like to know the most memory-efficient way to pull arbitrarily large data fields
I want to create a new form that is almost a duplicate of a

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.