» »

UTF-8 in Python

UTF-8 in Python

dunda ::

Tole je del kode:
....
list_of_files = glob.glob(potSlik + 'Ko*.jpg')
latest_file = max(list_of_files, key=os.path.getctime)
checksum1 = hashlib.md5(open(latest_file).read()).hexdigest()
....


Ta vrag deluje v Python 2. Sedaj pa zadevo selim v Python 3 in dobim napako:
Traceback (most recent call last):
  File "/home/user/webcam/webcam-ko.py", line 72, in <module>
    checksum1 = hashlib.md5(open(latest_file).read()).encode("utf-8").hexdigest()
  File "/usr/lib/python3.6/codecs.py", line 321, in decode
    (result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte


Kaj se je spremenilo v prehodu iz Python2 na Python3? In kako naj laik reši problem?
https://www.kacnje.eu

infiniteLoop ::

Ker jpg ni besedilo, ga moras odpreti kot binary file.

checksum1 = hashlib.md5(open(latest_file, mode='br').read()).hexdigest()


Potem bi moralo delati na trojki. Kako pa bi uredil da bi zadeva delala na 2 in 3 pa ne vem na pamet, bi moral iti guglat.
None of us is as dumb as all of us.

infiniteLoop ::

No evo sem poguglal, ce hoces, da zadeva dela v 2 in 3, moras uvoziti open iz io, potem se oba obnasata kot trojka.

from io import open

#...
checksum1 = hashlib.md5(open(latest_file, mode='br').read()).hexdigest()
None of us is as dumb as all of us.

dunda ::

Uf, hvala.
Zadošča mi, da deluje v 3ki. Ko bom vse prestavil na 3ko, bo delovalo tam.
https://www.kacnje.eu

HotBurek ::

Jst imam tako, da uporabim str.encode(), da string spremenim v byte:

checksum1 = hashlib.md5( str.encode( open(latest_file).read() ) ).hexdigest()

Pa uporabim hashlib.sha512()
root@debian:/# iptraf-ng
fatal: This program requires a screen size of at least 80 columns by 24 lines
Please resize your window

Zgodovina sprememb…

  • spremenilo: HotBurek ()

dunda ::

Tudi super. Hvala.
https://www.kacnje.eu

infiniteLoop ::

HotBurek je izjavil:

Jst imam tako, da uporabim str.encode(), da string spremenim v byte:

checksum1 = hashlib.md5( str.encode( open(latest_file).read() ) ).hexdigest()


Upam, da me ne boste smatrali za dlakocepa, ampak to bi delovalo samo za text file, jpg moras se vedno prebrati kot binary.
None of us is as dumb as all of us.

HotBurek ::

Nekaj je na tem, ja.

Jst imam tak setup, da ločim, kdaj berem text, kdaj pa slike (bajte). In potem pri branju uporabim "r" ali "rb".

file = open(path + "/favicon.ico", "rb");
content = file.read();
file.close();
root@debian:/# iptraf-ng
fatal: This program requires a screen size of at least 80 columns by 24 lines
Please resize your window

Zgodovina sprememb…

  • spremenilo: HotBurek ()


Vredno ogleda ...

TemaSporočilaOglediZadnje sporočilo
TemaSporočilaOglediZadnje sporočilo
»

[Python] HTTPS na desktopu dela, na Arduinu ne

Oddelek: Programiranje
91392 (1005) N4g4c3N
»

Komuniciranje med PHP in python

Oddelek: Programiranje
51167 (899) filips
»

Java skeniranje map in podmap

Oddelek: Programiranje
61146 (1035) nightrage
»

Generator enkratih gesel

Oddelek: Programiranje
7948 (781) fiction
»

Razbijanje gesel z GPU je realnost (strani: 1 2 3 )

Oddelek: Novice / Varnost
10230377 (24808) MrStein

Več podobnih tem