May-23-2022, 10:08 PM
Detecting float or int in a string
Detecting float or int in a string
|
May-23-2022, 10:09 PM
(May-23-2022, 09:49 PM)bowlofred Wrote: You're doing recursive function calls. Would need to test, but I'd worry that would have a bigger impact on memory than a try block. Good point. I will test it and see. I may have to unroll the recursion and test that too. (May-23-2022, 09:49 PM)bowlofred Wrote: I believe your function will pass the strings ++-+3 and 3e, while float() will not. Unrolling the recursion should allow me to correct that.
May-23-2022, 10:12 PM
May-24-2022, 04:57 AM
(May-23-2022, 08:49 PM)Clunk_Head Wrote: Can you please provide an example where an exception must be used in place of anything else? Python documentation, glossary: Quote:EAFP There are many countries out there with different notations. 1.000,42 and 1,000.42 are perfectly 'legal' and equal numbers, just depending your locale. There is ast.literal_eval() which can be used (locale thingy still applies): >>> import ast >>> ast.literal_eval('1.0') 1.0 >>> ast.literal_eval('+3') 3 >>> ast.literal_eval('-42') -42 >>> type(ast.literal_eval('-42')) <class 'int'>If you feed invalid value then you get SyntaxError: >>> ast.literal_eval('1.0.0') Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/ast.py", line 62, in literal_eval node_or_string = parse(node_or_string.lstrip(" \t"), mode='eval') File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/ast.py", line 50, in parse return compile(source, filename, mode, flags, File "<unknown>", line 1 1.0.0 ^^ SyntaxError: invalid syntaxYou can use EAFP style with try to literally evaluate value, catch syntax error if it's raised and do something about it
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
May-25-2022, 04:01 PM
Here's how I settled it to meet pythonic standards as well as those I was raised on.
In this application, a calculator, I was originally doing this: 1) Try to convert to float 2) except, check against operator list 3) if not in list, then it's an invalid expression But if I swap those checks around: 1) Check against operator list 2) if not in list, try to convert to float 3) except, invalid expression While minor it satisfies my desire to keep exceptions for exceptions. Thanks to all who replied, I learned a lot. I will be more open-minded about try/except blocks in python.
May-26-2022, 11:39 PM
(This post was last modified: May-26-2022, 11:40 PM by Pedroski55.)
I don't know, but I suspect, behind the scenes, within isnumeric(), re is being used anyway.
Is there another, better way to find numbers? Why not just use re explicitly? Then you can see exactly what is going on. import re things = [1.5, '2.5', 3.5, '4.5', 'a.bc', 'cde. do', 2, 7, '2.A', '2.2.', '2.A2', 1.56, 2.77, '3.33.33'] my_pattern = re.compile("\d+\.\d+") for t in things: match_f = re.search(my_pattern, str(t)) print(t, match_f)And with the output of match_f.span() you can slice the number from a string! |
|
Users browsing this thread: 1 Guest(s)