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"
4
  • What's the expected output??? Commented Mar 18, 2014 at 10:26
  • @Jayanth Koushik : It's ok to get a float/int output. Commented Mar 18, 2014 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. Commented Mar 18, 2014 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? Commented Mar 18, 2014 at 10:31

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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)
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). The hashlib functions are designed to always give the same output for the same input.
0

You can use hash function:

>>> hash("Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:27.0) Gecko/20100101 Firefox/27.0")
1892010093

5 Comments

Hey ndpu, I get negative values? for the abouve one I got -3723762795795855251.
yeah; hash depends on a lot of things. it is possible for the value to be negative.
@NilaniAlgiriyage check this question stackoverflow.com/questions/793761/…
@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.
@ndpu : Yes, hashing works :) Thanks...abs(hash(s)) % (10 ** 8) can be used to get 8 digit representation(or any number of digits).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.