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

  • Home
  • SEARCH
  • 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 8543603
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T12:20:41+00:00 2026-06-11T12:20:41+00:00

Suppose I have a text file with numbers separated by colons and spaces as

  • 0

Suppose I have a text file with numbers separated by colons and spaces as the following.

0:-83 1: -51 2: -69 3: -82 4: -85 8: -90 9: -69 QUAD
0:-88 1: -88 2: -98 3: -75 4: -42 5: -71 6: -89 7: -28 8: -83 9: -78 STADIUM

A pair is defined as two numbers separated by a colon. Spaces interrupt pairs of numbers arbitrarily.

Currently, I have the following.

with open('data.txt') as file:
  lines = file.read().splitlines()
for line in lines: 
  line = line[:-1]  
  # What is the regex I should be using? 
  # data = re.split(r'[:\s]',line) includes the space after the colon if it exists

What is the best way to parse the text file so that each line is stored as a list of tuples where each tuple is a pair?

  • 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-11T12:20:43+00:00Added an answer on June 11, 2026 at 12:20 pm

    The following regex will give you your pairs of numbers (including minus signs) in groups:

    r'\b(-?\d+)\s*:\s*(-?\d+)\b'
    

    This matches a word-boundary (\b), then a set of digits (with an optional - minus sign before it), followed by a colon surrounded by optional whitespace, followed by another set of digits with optional minus sign, followed by a word boundary.

    Demo:

    >>> import re
    >>> numpairs = re.compile(r'\b(-?\d+)\s*:\s*(-?\d+)\b')
    >>> example = '0:-83 1: -51 2: -69 3: -82 4: -85 8: -90 9: -69 QUAD'
    >>> numpairs.findall(example)
    [('0', '-83'), ('1', '-51'), ('2', '-69'), ('3', '-82'), ('4', '-85'), ('8', '-90'), ('9', '-69')]
    

    You probably don’t want to reach all lines into memory at once; just loop over your file line by line:

    import re
    numpairs = re.compile(r'\b(-?\d+)\s*:\s*(-?\d+)\b')
    with open('data.txt') as file:
        for line in file:
            for pair in numpairs.findall(line):
                print pair
    

    You don’t need to remove the newline for the above code to work, but if you do, use line.strip() instead of line[:-1].

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

Sidebar

Related Questions

suppose i have the following in a text file (or dired buffer) open in
Suppose I have a text file with data separated by whitespace into columns. I
I have a text file that contains two lines of numbers, all I want
Suppose you have a string with text in two or more scripts. When you
Suppose I have two h:inputText components. I want to bind both of the text
Suppose I have a text file with some data I want to retrieve lost
Suppose I have a text file that says: This file has plain text Now,
Suppose I have a string such as this in a text file: (((var1 AND
Suppose I have a text file with this content: abcdefghk I want to write
Suppose I have a 400K text file which I want to read from 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.