Sunday, March 27, 2011

Re: how to parse string in django

On Sunday, March 27, 2011 07:38:46 pm django beginner wrote:
> hi django experts,
>
> how do I strip the following string in python (Charfield format)
>
> 14:00
> 3:00
>
> wherein I could compare the results - convert in integer:
>
> if 14 > 3 . . .
>
> thanks in advance!

a = "14:00"
b = "3:00"

def parseTime(s)
# split them at ":"
try:
split = s.split(':')
except:
raise ValueError("{0} is not in the right format.".format(s)

if split[0].isdigit():
# explicitly cast string val to an int to get an int.
i = int(split[0])
else:
raise TypeError("{0} is not an int.".format(split[0])

return i

if parseTime(a) > parseTime(b):
# do stuff.


string methods are cool: http://docs.python.org/library/stdtypes.html#string-
methods


Mike

--
We can defeat gravity. The problem is the paperwork involved.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment