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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T01:48:42+00:00 2026-06-06T01:48:42+00:00

I have a tab delimited text file with the following data: ahi1 b/se ahi

  • 0

I have a tab delimited text file with the following data:

    ahi1
    b/se
ahi 
test    -2.435953
        1.218364
    ahi2
    b/se
ahi 
test    -2.001858
        1.303935

I want to extract the two floating point numbers to a separate csv file with two columns, ie.

-2.435953 1.218264

-2.001858 1.303935

Currently my hack attempt is:

 import csv
 from itertools import islice
 results = csv.reader(open('test', 'r'), delimiter="\n")

 list(islice(results,3))
 print results.next()
 print results.next()
 list(islice(results,3))
 print results.next()
 print results.next()

Which is not ideal. I am a Noob to Python so I apologise in advance and thank you for your time.

  • 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-06T01:48:44+00:00Added an answer on June 6, 2026 at 1:48 am

    Here is the code to do the job:

    import re
    
    # this is the same data just copy/pasted from your question
    data = """    ahi1
        b/se
    ahi 
    test    -2.435953
            1.218364
        ahi2
        b/se
    ahi 
    test    -2.001858
            1.303935"""
    
    # what we're gonna do, is search through it line-by-line
    # and parse out the numbers, using regular expressions
    
    # what this basically does is, look for any number of characters
    # that aren't digits or '-' [^-\d]  ^ means NOT
    # then look for 0 or 1 dashes ('-') followed by one or more decimals
    # and a dot and decimals again: [\-]{0,1}\d+\.\d+
    # and then the same as first..
    pattern = re.compile(r"[^-\d]*([\-]{0,1}\d+\.\d+)[^-\d]*")
    
    results = []
    for line in data.split("\n"):
        match = pattern.match(line)
        if match:
            results.append(match.groups()[0])
    
    pairs = []
    i = 0
    end = len(results)
    while i < end - 1:
        pairs.append((results[i], results[i+1]))
        i += 2
    
    for p in pairs:
        print "%s, %s" % (p[0], p[1])
    

    The output:

    >>>
    -2.435953, 1.218364
    -2.001858, 1.303935
    

    Instead of printing out the numbers, you could save them in a list and zip them together afterwards..
    I’m using the python regular expression framework to parse the text. I can only recommend you pick up regular expressions if you don’t already know it. I find it very useful to parse through text and all sorts of machine generated output-files.

    EDIT:

    Oh and BTW, if you’re worried about the performance, I tested on my slow old 2ghz IBM T60 laptop and I can parse a megabyte in about 200ms using the regex.

    UPDATE:
    I felt kind, so I did the last step for you 😛

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

Sidebar

Related Questions

I have a text file with tab delimited data spread across 16 columns. I
I have to parse a tab-delimited text file with Ruby to extract some data
I have the following like data in a tab-delimted text file named original Name
Let's say I have a tab-delimited text file that contains data arranged in columns
I have a 2GB big text file, it has 5 columns delimited by tab.
I have a text file that is tab-delimited. How can I separate this string
I have a text file that is tab delimited and looks like: 1_0 NP_045689
I read in a text file that is tab delimited, i then have a
I have a tab-delimited text file that I am parsing. Its first column contains
I have a tab-delimited text file. I have split this into columns. Each of

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.