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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T03:51:25+00:00 2026-05-15T03:51:25+00:00

I’d like convert strings to floats using Python 2.6 and later, but without silently

  • 0

I’d like convert strings to floats using Python 2.6 and later, but without silently converting things like 'NaN' and 'Inf' to float objects. I do want them to be silently ignored, as with any text that isn’t valid as a float representation.

Before 2.6, float("NaN") would raise a ValueError on Windows. Now it returns a float for which math.isnan() returns True, which is not useful behaviour for my application. (As was pointed out, this has always been a platform-dependent behaviour, but consider it an undesirable behaviour for my purposes, wherever it happens.)

Here’s what I’ve got at the moment:

import math
def get_floats(source):
    for text in source.split():
        try:
            val = float(text)
            if math.isnan(val) or math.isinf(val):
                raise ValueError
            yield val
        except ValueError:
            pass

This is a generator, which I can supply with strings containing whitespace-separated sequences representing real numbers. I’d like it to yield only those fields which are purely numeric representations of floats, as in “1.23” or “-34e6”, but not for example “NaN” or “-Inf”. Things that aren’t floats at all, e.g. “hello”, should be ignored as well.

Test case:

assert list(get_floats('1.23 foo -34e6 NaN -Inf')) == [1.23, -34000000.0]

Please suggest alternatives you consider more elegant, even if they involve “look before you leap” (which is normally considered a lesser approach in Python).

Edited to clarify that non-float text such as “hello” should just be ignored quietly as well. The purpose is to pull out only those things that are real numbers and ignore everything else.

  • 1 1 Answer
  • 1 View
  • 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-05-15T03:51:25+00:00Added an answer on May 15, 2026 at 3:51 am

    I’d write it like this. I think it combines conciseness with readability.

    def is_finite(x):
        return not math.isnan(x) and not math.isinf(x)
    
    def get_floats(source):
        for x in source.split():
            try:
                yield float(x)
            except ValueError:
                pass
    
    def get_finite_floats(source):
        return (x for x in get_floats(source) if is_finite(x))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 501k
  • Answers 501k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer First of all, Sourcesafe will the support for Visual Sourcesafe… May 16, 2026 at 2:24 pm
  • Editorial Team
    Editorial Team added an answer Yes, you need to do this - "...like '%" +… May 16, 2026 at 2:24 pm
  • Editorial Team
    Editorial Team added an answer Thanks Chris Heald for your response. I did end up… May 16, 2026 at 2:24 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I want to count how many characters a certain string has in PHP, but
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have text I am displaying in SIlverlight that is coming from a CMS
I used javascript for loading a picture on my website depending on which small
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on
I have a JSP page retrieving data and when single or double quotes are
Does anyone know how can I replace this 2 symbol below from the string

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.