473,809 Members | 2,703 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

pre-check for string-to-number conversion

I am reading an ASCII data file and converting some of the strings to
integers or floats. However, some of the data is corrupted and the
conversion doesn't work. I know that I can us exceptions, but they
don't seem like the cleanest and simplest solution to me. I would like
to simply perform a pre-check before I do the conversion, but I can't
figure out how to do it. I see that strings have a function called
isdigit(), but that simply checks whether all characters in the string
are digits -- not what I need. Any suggestions? Thanks.

Jul 18 '05 #1
3 1586
18********@snea kemail.com wrote:
I am reading an ASCII data file and converting some of the strings to
integers or floats. However, some of the data is corrupted and the
conversion doesn't work. I know that I can us exceptions, but they
don't seem like the cleanest and simplest solution to me.
You should reconsider this thought. It's quite clean and fast:

for s in get_ASCII_strin gs_from_data_fi le():
try:
f = float(s)
except ValueError:
do_whatever_you _need_to_do_for _invalid_data()

I would like to simply perform a pre-check before I do the
conversion, but I can't figure out how to do it.


Again, this is the less Pythonic approach, but one option would be to
use regular expressions:

matcher = re.compile(r'[\d.]+')
for s in get_ASCII_strin gs_from_data_fi le():
if matcher.match(s ):
f = float(s)
else:
do_whatever_you _need_to_do_for _invalid_data()

but note that this won't except valid floats like '1e10'. You'll also
note that this is actually less concise than the exception based approach.

Steve

Jul 18 '05 #2
18********@snea kemail.com wrote:
I know that I can us exceptions, but they
don't seem like the cleanest and simplest solution to me.


Stop worrying and learn to love exceptions. :)
--
Michael Hoffman
Jul 18 '05 #3
Yes, I suppose exceptions are the best way to handle this problem.
Thanks.

Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
18541
by: Alan Illeman | last post by:
How do I set several different properties for PRE in a CSS stylesheet, rather than resorting to this: <BODY> <PRE STYLE="font-family:monospace; font-size:0.95em; width:40%; border:red 2px solid; color:red;
8
3807
by: Jarno Suni not | last post by:
It seems to be invalid in HTML 4.01, but valid in XHTML 1.0. Why is there the difference? Can that pose a problem when such a XHTML document is served as text/html?
3
5084
by: Leiji Liu | last post by:
Hi, I am curious if there are any pre-defined varables (constants?) in C? I saw some code with __LINE__, __FILE__, etc. Are those located in some include files? LL
6
1933
by: John | last post by:
I have two books saying different things about the #region directive. One says that all code within #region does not affect the compilation process in any way, while the other states that the entire region is marked for a pre-processor directive which does affect the compilation process. I was wondering which is accurate? Is #region/#endregion just a text editor visualization tool with not effect otherwise, or is everything within the...
0
1091
by: L | last post by:
Hi, I am trying NDoc for the first time and here is my problem. In my project, I am using <summary> <pre> my text here in specific format. hello. etc... </pre>
1
2328
by: Arthur Dent | last post by:
Hello all, ... question for those who've been playing with the 2005 pre-releases: Does VB.NET have pre & post-build events in 2005 by any chance? This would be such a a great improvement. Thanks in advance, - Arthur Dent.
0
916
by: Edwin Knoppert | last post by:
I often force a format of the code and HTML. I finally figured out why my PRE tagged code resulted in wrapped lines. It was the IDE not seeing it is PRE /PRE code and still wraps it.
7
4860
by: Paul Connolly | last post by:
char *s = "Hello"; s = 'J'; puts(s); might print "Jello" in a pre-ANSI compiler - is the behaviour of this program undefined in any pre-ANSI compiler - or would it always have printed "Jello" with a pre-ANSI compiler? In gcc with the "writable-strings" option this program prints Jello If there were more than one semantics for what this progran did under a
4
3992
by: APEJMAN | last post by:
would you please help me with this question? I know that a binary tree can be recovered from its pre-order traversal. That is, a tree built from the pre-order traversal should always be the same as the original tree. Is it true that the pre-order traversal tells the order in which the values were inserted?
5
6985
by: Hendrik Maryns | last post by:
Hi, A friend complained that the border around the lower code part on http://tcl.sfs.uni-tuebingen.de/~hendrik/keyboard.shtml is too small for the content. This is because his window size is so small that the size of the containing <preis smaller than the text in it. Since it is <pre>, the text is not wrapped, but the border and background color get smaller. Thus the text pops out of its box. Is there something one can do against...
0
9601
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10376
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10379
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10115
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9199
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7660
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6881
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4332
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3014
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.