Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > pl.comp.programming > #29045

Re: python i stringi

From Piotr Chamera <piotr_chamera@poczta.onet.pl>
Newsgroups pl.comp.programming
Subject Re: python i stringi
Date 2016-10-06 10:57 +0200
Organization A noiseless patient Spider
Message-ID <nt53kl$ia8$1@dont-email.me> (permalink)
References <19jwk9uqtvyxa.dlg@tyczka.com>

Show all headers | View raw


W dniu 2016-10-06 o 09:46, Roman Tyczka pisze:
> Witam,
>
> Używam czasami pythonowej konsoli jako kalkulatora, teraz zapragnąłem
> policzyć md5 unicodowego stringa, jednak nie znam pythona jeszcze zbytnio i
> się zawiesiłem:
>
>>>> import hashlib
>>>> ustr = u"Jeszcze dłuższy i bardzo długi string z ółęść oraz ;%$#@[ i innymi znakami 0099786764553 na końcu"
>>>> type(ustr)
> <type 'unicode'>
>>>> hashlib.md5(ustr).hexdigest()
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> UnicodeEncodeError: 'ascii' codec can't encode character u'\u0142' in
> position 9: ordinal not in range(128)
>
> Jak wybrnąć i policzyć jednak tego hasha?
>
> ps. python 2.7.9 na windows
>

np tak:
 >>> hashlib.md5(ustr.encode(encoding="utf-8", errors="strict")).hexdigest()

'32857a03910f204bd5adc4cb2ec2bce8'

lub tak:
 >>> hashlib.md5(ustr.encode(encoding="utf-16", 
errors="strict")).hexdigest()

'5da8f084e4da17f9441beccf2fa8b437'

czyli trzeba jawnie skonwertować tekst unicode do takiego formatu, z 
jakiego chcemy liczyć md5

Back to pl.comp.programming | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

python i stringi Roman Tyczka <noemail@because.no> - 2016-10-06 09:46 +0200
  Re: python i stringi godek.maciek@gmail.com - 2016-10-06 01:10 -0700
    Re: python i stringi Roman Tyczka <noemail@because.no> - 2016-10-06 10:19 +0200
      Re: python i stringi godek.maciek@gmail.com - 2016-10-06 01:32 -0700
        Re: python i stringi Roman Tyczka <noemail@because.no> - 2016-10-06 10:44 +0200
          Re: python i stringi Piotr Chamera <piotr_chamera@poczta.onet.pl> - 2016-10-06 12:14 +0200
            Re: python i stringi Roman Tyczka <noemail@because.no> - 2016-10-06 12:32 +0200
              Re: python i stringi Piotr Chamera <piotr_chamera@poczta.onet.pl> - 2016-10-06 13:22 +0200
  Re: python i stringi Piotr Chamera <piotr_chamera@poczta.onet.pl> - 2016-10-06 10:57 +0200
    Re: python i stringi Roman Tyczka <noemail@because.no> - 2016-10-06 11:22 +0200
      Re: python i stringi Piotr Chamera <piotr_chamera@poczta.onet.pl> - 2016-10-06 11:50 +0200

csiph-web