2

We can convert a datetime value in to decimal using following function.

import time
from datetime import datetime

t = datetime.now()
t1 = t.timetuple()

print time.mktime(t1)

Output :

Out[9]: 1395136322.0

Similarly is there a way to convert strings in to a decimal using python?.

Example string.

"Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:27.0) Gecko/20100101 Firefox/27.0"
Nilani Algiriyage
  • 32,876
  • 32
  • 87
  • 121
  • What's the expected output??? – Jayanth Koushik Mar 18 '14 at 10:26
  • @Jayanth Koushik : It's ok to get a float/int output. – Nilani Algiriyage Mar 18 '14 at 10:29
  • 1
    Maybe you are mis-understanding ``time.mktime()``. It returns the number of seconds since Jan 1, 1970. Thus instead of representing dates as (day, month, year) since Jesus, it represents times/dates as seconds since 1970. There's not really an analogous representation for arbitrary strings. – Christian Aichinger Mar 18 '14 at 10:29
  • @Christian Aichinger : It's ok it shows the numberof seconds, I just need get a numerical value for a string, and always if the same string is presented same numerical value should be produced? – Nilani Algiriyage Mar 18 '14 at 10:31

2 Answers2

3

If you want an integer to uniquely identify a string, I'd go for hashing functions, like SHA. They return the same value for the same input.

import hashlib
def sha256_hash_as_int(s):
    return int(hashlib.sha256(s).hexdigest(), 16)

If you use Python 3, you first have to encode s to some concrete encoding, like UTF-8.

Furthermore, take a look at the hashlib module and decide if you really need an integer, or if the output of hexdigest() isn't OK for you, too.

Christian Aichinger
  • 6,989
  • 4
  • 40
  • 60
  • What about this? abs(hash(s)) % (10 ** 8) Will it always generate same number for same string.(Up to now my experiment strings always gave me the same number) – Nilani Algiriyage Mar 18 '14 at 10:44
  • 1
    It depends on your needs. ``hash(s)`` isn't constant between different Python versions and even between different Python sessions (newer Python versions only, due to [hash randomization](http://bugs.python.org/issue13703)). The ``hashlib`` functions are designed to always give the same output for the same input. – Christian Aichinger Mar 18 '14 at 10:51
0

You can use hash function:

>>> hash("Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:27.0) Gecko/20100101 Firefox/27.0")
1892010093
ndpu
  • 22,225
  • 6
  • 54
  • 69
  • Hey ndpu, I get negative values? for the abouve one I got -3723762795795855251. – Nilani Algiriyage Mar 18 '14 at 10:28
  • yeah; hash depends on a lot of things. it is possible for the value to be negative. – Jayanth Koushik Mar 18 '14 at 10:30
  • @NilaniAlgiriyage check this question http://stackoverflow.com/questions/793761/built-in-python-hash-function – ndpu Mar 18 '14 at 10:32
  • 1
    @NilaniAlgiriyage Note that the `hash` might not be always the same for the same string. If hash randomization is turned on different executions of the interpreter will result in different hashes. – Bakuriu Mar 18 '14 at 10:37
  • 1
    @ndpu : Yes, hashing works :) Thanks...abs(hash(s)) % (10 ** 8) can be used to get 8 digit representation(or any number of digits). – Nilani Algiriyage Mar 18 '14 at 10:38