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

The Archive Base Latest Questions

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

I am parsing some data where the standard format is something like 10 pizzas

  • 0

I am parsing some data where the standard format is something like 10 pizzas. Sometimes, data is input correctly and we might end up with 5pizzas instead of 5 pizzas. In this scenario, I want to parse out the number of pizzas.

The naïve way of doing this would be to check character by character, building up a string until we reach a non-digit and then casting that string as an integer.

num_pizzas = ""
for character in data_input:
   if character.isdigit():
      num_pizzas += character
   else:
      break
num_pizzas = int(num_pizzas)

This is pretty clunky, though. Is there an easier way to split a string where it switches from numeric digits to alphabetic characters?

  • 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-15T06:08:36+00:00Added an answer on June 15, 2026 at 6:08 am

    You ask for a way to split a string on digits, but then in your example, what you actually want is just the first numbers, this done easily with itertools.takewhile():

    >>> int("".join(itertools.takewhile(str.isdigit, "10pizzas")))
    10
    

    This makes a lot of sense – what we are doing is taking the character from the string while they are digits. This has the advantage of stopping processing as soon as we get to the first non-digit character.

    If you need the later data too, then what you are looking for is itertools.groupby() mixed in with a simple list comprehension:

    >>> ["".join(x) for _, x in itertools.groupby("dfsd98sd8f68as7df56", key=str.isdigit)]
    ['dfsd', '98', 'sd', '8', 'f', '68', 'as', '7', 'df', '56']
    

    If you then want to make one giant number:

    >>> int("".join("".join(x) for is_number, x in itertools.groupby("dfsd98sd8f68as7df56", key=str.isdigit) if is_number is True))
    98868756
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing some CSV data in C for the purposes of a Ruby extension.
I am currently parsing some xml that looks like this <Rows> <Row MANUFACTURERID=76 MANUFACTURERNAME=Fondont
Sometimes I want to attach some kind of meta data to a HTML node
I'm trying to parse some data in a fixed format text file where each
Consider a standard ASP.NET web application where the user types in some numeric data
Has anyone ever worked with a system of passing back say, some JSON data
I'm having some problems with Jquery UI Dialog and TinyMCE. Passing data to a
I'm parsing some json to return a basic string token or error message. -
I am parsing some HTML source. Is there a regex script to find out
I'm parsing some file with XmlPullParser in Android Everything goes fine except for some

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.