473,804 Members | 2,715 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Converting a string to the most probable type

Hi,

I would like to know if there is a module that converts a string to a
value of the "most probable type" ; for instance :
- if the string is "abcd" the value is the same string "abcd"
- string "123" : value = the integer 123
- string "-1.23" (or "-1,23" if the locale for decimals is ,) : value
= the float -1.23
- string "2008/03/06" (the format is also locale-dependant) : value =
datetime.date(2 008,03,06)

Like in spreadsheets, special prefixes could be used to force the
type : for instance '123 would be converted to the *string* "123"
instead of the *integer* 123

I could code it myself, but this wheel is probably already invented

Regards,
Pierre
Mar 6 '08
13 1786
On Mar 8, 12:05*pm, Pierre Quentel <quentel.pie... @wanadoo.frwrot e:
>>def convert(x):
* * * * if '.' in x:
* * * * * * * * try: return float(x)
* * * * * * * * except ValueError: return x
* * * * else:
* * * * * * * * try: return int(x)
* * * * * * * * except: return x
>>convert('123' )
123
>>convert('123. 99')
123.98999999999 999
>>convert('hell o')

Hi,

That's fine for people who write floats with a "." ; but others learn
to enter them with ","

For the same float, the French write the literal 123.456.789,99 when
others write 123,456,789.99 ; for us, today is 8/3/2008 (or
08/03/2008) where for others it's 3/8/2008 or perhaps 2008/3/8

Popular spreadsheets know how to "guess" literals ; if the guess is
not correct users can usually specify the pattern to use. My question
was just to know if something similar had already been developed in
Python ; I understand that the answer is no

Thanks,
Pierre
In particular, you can retain access to the user/interface, and always
just query when probabilities aren't 100%. In general, retain access
to a higher power, such as offering a hook ( def butdonotguess( raw,
guesses ):). If you're making the decision, and a case-by-case is too
expensive, then you've made a policy, and someone gets shaft "'cause
policy". You can throw an exception that's guaranteed to be handled
or exits. '121212' isn't as bad as '121110'! If you want to find out
who and when writes dates like that, apply to PR. Obviously a person
knew at one point what the string was; how much is left?

P.S. def welltheydidthat thistime( raw, guesses ):.
Mar 8 '08 #11
The trick in the case of when you do not want to guess, or the choices
grow too much, is to ask the user to tell you in what format they want
it and format according to their wishes.

Neatly avoids too much guessing and isn't much extra to add.
Mar 10 '08 #12
On Mar 11, 3:19 am, cokofree...@gma il.com wrote:
The trick in the case of when you do not want to guess, or the choices
grow too much, is to ask the user to tell you in what format they want
it and format according to their wishes.

Neatly avoids too much guessing and isn't much extra to add.
The plot is about understanding input, not formatting output.

Mar 10 '08 #13
Lie
On Mar 11, 4:15*am, John Machin <sjmac...@lexic on.netwrote:
On Mar 11, 3:19 am, cokofree...@gma il.com wrote:
The trick in the case of when you do not want to guess, or the choices
grow too much, is to ask the user to tell you in what format they want
it and format according to their wishes.
Neatly avoids too much guessing and isn't much extra to add.

The plot is about understanding input, not formatting output.
And what he meant is simply to make an agreement with the user on how
he/she would format his/her input and to disallow input from formats
that haven't been agreed to avoid guessing. That is the cleanest and
most polite solution, although I'd suspect it would be considered less
user friendly by regular user although power user would be most happy
with that.
Mar 16 '08 #14

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

Similar topics

5
13930
by: matt melton | last post by:
Hi there, I am trying to write a method that accepts an array of any primitive type and will return the same array without copying memory as an array of bytes. ie. I'd like to be able to do something like: char chars = "Hello!"; byte bytes = (byte) chars;
19
6174
by: Erik Wikström | last post by:
First of all, forgive me if this is the wrong place to ask this question, if it's a stupid question (it's my second week with C++), or if this is answered some place else (I've searched but not found anything). Here's the problem, I have two sets of files, the name of a file contains a number which is unique for each set but it's possible (even probable) that two files in different sets have the same numbers. I want to store these...
45
5326
by: Rakesh | last post by:
Hi, I have this function to reverse the given string. I am just curious if that is correct and there could be better way of doing it / probable bugs in the same. The function prototype is similar to the one in any standard C library. <---- Code starts -->
2
5755
by: Asbjørn Ulsberg | last post by:
Hi. I'm trying to convert Brady Hegberg's great RTF2HTML VB 6.0 module to C#. I've managed to convert the VB code to VB.NET, which gave me the following code: Option Strict On Option Explicit On Option Compare Binary
21
8368
by: oksuresh | last post by:
Hi talents, I have noticed that atof() function approximates the string I pass to it. when I use atof() , as atof(" 184.64") and it returns 184.63999999999 But I would like to have the exact value that is passed.
9
2580
by: Terry | last post by:
I am converting (attempting) some vb6 code that makes vast use of interfaces. One of the major uses is to be able to split out Read-only access to an obect. Let me give you a simple (contrived) example: In Project RoObjDefs: RoPerson.cls file: Public Property Get FirstName() as String Public Property Get LastName() as String <end of file RoPerson.cls> RoPersons.cls file Public Function Count() as Integer
2
4011
by: CoreyWhite | last post by:
Problem: You have numbers in string format, but you need to convert them to a numeric type, such as an int or float. Solution: You can do this with the standard library functions. The functions strtol, strtod, and strtoul, defined in <cstdlib>, convert a null- terminated character string to a long int, double, or unsigned long. You can use them to convert numeric strings of any base to a numeric
2
11187
by: Brian Parker | last post by:
I am beginning to work with VB2005.NET and I'm getting some problems with string formatting converting an application from VB6. VB6 code:- sTradeDate = Format(pArray(4,i Record), "mmddyy") pArray is a variant array containing a date string at pArray(4, iRecord) in the format "yyyy/mm/dd"
3
1820
by: Dhananjay | last post by:
Hi All, I am facing problem when i am converting C#.net code(Delegate concept) into vb.net. I am unable to do that . Can someone help me to solve the problem. I am providing my C#.net code. ==================================my code is :- ====================================== static public List<MembershipUserWrapperGetMembers(bool
0
9704
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9571
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
10561
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10069
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...
1
7608
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
6845
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();...
0
5505
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5639
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4277
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

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.