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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T17:18:02+00:00 2026-06-10T17:18:02+00:00

I am trying to parse a text file with the following structure: latitude 5.0000

  • 0

I am trying to parse a text file with the following structure:

latitude                        5.0000
number_of_data_values             9
  0.1   0.2   0.3   0.4
  1.1   1.2   1.3   1.4      
  8.1
latitude                        4.3000
number_of_data_values             9
  0.1   0.2   0.3   0.4
  1.1   1.2   1.3   1.4       
  8.1
latitude                        4.0000
number_of_data_values             9
  0.1   0.2   0.3   0.4
  1.1   1.2   1.3   1.4       
  8.1
 ...

Every different latitude number is a different array line.
number_of_data_values is the number of colomns (consistent thorough the file).

For this example I would like to read the file and output a 3 by 9 two-dimensional array like the following:

array = [[0.1,0.2,0.3,0.4,1.1,1.2,1.3,1.4,8.1],
         [0.1,0.2,0.3,0.4,1.1,1.2,1.3,1.4,8.1],
         [0.1,0.2,0.3,0.4,1.1,1.2,1.3,1.4,8.1]]

I had a try at it by iterating through the line with loops but I am looking for a more efficient way to do it as I may deal with voluminous input files.

  • 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-10T17:18:04+00:00Added an answer on June 10, 2026 at 5:18 pm

    A line-by-line implementation is rather easy and understandable. Assuming that your latitude always start on a new line (which is not what your example give, but it might be a typo), you could do:

    latitudes = []
    counts = []
    blocks = []
    current_block = []
    for line in test:
        print line
        if line.startswith("latitude"):
            # New block: add the previous one to `blocks` and reset
            blocks.append(current_block)
            current_block = []
            latitudes.append(float(line.split()[-1]))
        elif line.startswith("number_of_data"):
            # Just append the current count to the list
            counts.append(int(line.split()[-1]))
        else:
            # Update the current block
            current_block += [float(f) for f in line.strip().split()]
    # Make sure to add the last block...
    blocks.append(current_block)
    # And to remove the first (empty) one
    blocks.pop(0)
    

    You can know check whether all your blocks have the proper size:

    all(len(b)==c for (c,b) in zip(counts,blocks))
    

    Alternative solution

    If you’re concerned about the loops, you may want to consider querying a memory-mapped version of your file. The idea is to find the positions of the lines starting with latitude. Once you find one, find the next and you have a block of text: zap the first two lines (the one starting with latitude and the one starting with number_of_data), combine the remaining ones and process.

    import mmap
    
    with open("crap.txt", "r+b") as f:
        # Create the mapper
        mapper = mmap.mmap(f.fileno(), 0)
        # Initialize your output variables
        latitudes = []
        blocks = [] 
        # Find the beginning of the first block
        position = mapper.find("latitude")
        # `position` will be -1 if we can't find it
        while (position >= 0):
            # Move to the beginning of the block
            mapper.seek(position)
            # Read the first line
            lat_line = mapper.readline().strip()
            latitudes.append(lat_line.split()[-1])
            # Read the second one
            zap = mapper.readline()
            # Where are we ?
            start = mapper.tell()
            # Where's the next block ?
            position = mapper.find("latitude")
            # Read the lines and combine them into a large string
            current_block = mapper.read(position-start).replace("\n", " ")
            # Transform the string into a list of floats and update the block
            blocks.append(list(float(i) for i in current_block.split() if i))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to parse out a text file that looks like the following:
so I'm trying to upload then parse a text file with the following format:
I'm trying to read a text file built with the following format in every
I'm trying to parse a text file using parser combinators. I want to capture
I'm trying to parse text-based file attachments (txt, doc, etc...). However, I can't seem
I'm trying to parse and read a text file in Windows Phone 7. Here
I'm working on a 1 Gigabyte JSON text file which I'm trying to parse
I am trying to parse following text in variable... $str = 3,283,518(10,569 / 2,173)
I have an XML file in the following structure. <NewDataSet> <markers> <name>name text</name> <desc>desc
So I'm trying to a parse a file that has text in this format:

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.