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


Groups > de.comp.lang.python > #5530

Re: [Python-de] python2 --> python3 : str vs bytes

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Stefan Behnel <python-de@behnel.de>
Newsgroups de.comp.lang.python
Subject Re: [Python-de] python2 --> python3 : str vs bytes
Date Sun, 25 Aug 2019 10:44:02 +0200
Lines 27
Message-ID <mailman.41.1566723043.30344.python-de@python.org> (permalink)
References <qjtf7q$p8s$1@news2.informatik.uni-stuttgart.de> <07330df3-6eec-a92c-40c5-71634b718f33@behnel.de>
Mime-Version 1.0
Content-Type text/plain; charset=utf-8
Content-Transfer-Encoding 8bit
X-Trace news.uni-berlin.de 8tzmcol23VuPPXI8xhN18wPTQ9cVSX60zsghiINyNEKw==
Return-Path <python-de@behnel.de>
X-Original-To python-de@python.org
Delivered-To python-de@mail.python.org
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; t=1566723003; s=strato-dkim-0002; d=behnel.de; h=In-Reply-To:Date:Message-ID:From:References:To:Subject: X-RZG-CLASS-ID:X-RZG-AUTH:From:Subject:Sender; bh=6Ir1waAelQTxRcILR4o37mJbS2Zzwi5B5MKt3XVu+JU=; b=Cc5TDuxm9/JkVhgLDV2+Ks3tICewdNSUFZEBbiYcH9r+KIWauwQ3E6+auRJUAJ4FT3 rQQAYRMGYn3msZv40ROTRKn3I3VEzLEqj5Mt7xL8dBnWK42WR4kxOXPx9xerlY6wG5F6 kak4qi9GkW7el8+C+rOIfr0dN1Qdz3dhw2mAgca+xOnZWftSm2gA5xxMdjgBJwHUyD2U fTdODDtfLIYICcw4HZdoO0LQOjnYLqmSBWLnwcLHxEXUqaIHVJa7Cx5W2egQTfHYxroI HgA+I/jJt04ACiythC/drfOHVwEQgHgVpuuXvuxvBXR5minjMsJFO/S/Kwb/BmHnSFyo wtcQ==
X-RZG-AUTH ":E1MMdFW4b++AXZOTwA41DOYM0Dv9LNWvavC/fJZ6Wfgmp/Lh1ANWCRaaq2R1hCooD/t2Vl9QaFHh38sJGCJu0nqS0iI1gBy4Wj8LVlpcjbXQEUY="
X-RZG-CLASS-ID mo00
Openpgp preference=signencrypt
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.8.0
In-Reply-To <qjtf7q$p8s$1@news2.informatik.uni-stuttgart.de>
Content-Language de-DE
X-BeenThere python-de@python.org
X-Mailman-Version 2.1.29
Precedence list
List-Id Die Deutsche Python Mailingliste <python-de.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-de>, <mailto:python-de-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-de/>
List-Post <mailto:python-de@python.org>
List-Help <mailto:python-de-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-de>, <mailto:python-de-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID <07330df3-6eec-a92c-40c5-71634b718f33@behnel.de>
X-Mailman-Original-References <qjtf7q$p8s$1@news2.informatik.uni-stuttgart.de>
Xref csiph.com de.comp.lang.python:5530

Show key headers only | View raw


Ulli Horlacher schrieb am 25.08.19 um 10:00:
> TypeError: sequence item 0: expected str instance, bytes found
> 
> Wie macht mans besser?

Grundregel: die Eingabedaten immer gleich beim Einlesen dekodieren und erst
beim Ausgeben wieder kodieren, damit du innerhalb deines Programmes überall
mit Unicode (sprich: Text) arbeiten kannst.

Das Dekodieren erledigt Python (3) oft auch automatisch, z.B. wenn du von
stdin oder aus einer UTF8-Textdatei (oder JSON/XML/...) liest. Alles andere
musst du aber explizit einstellen, z.B. bei "open()" mit der "encoding=..."
Option.

Das Kodieren passiert z.B. beim Schreiben nach stdout (oder stderr) auch
automatisch, in anderen Fällen musst du es aber explizit machen,
insbesondere beim Schreiben in Dateien, denn Python kann nicht wissen, ob
du die Datei in UTF-8 oder EBCDIC kodiert haben möchtest.

In deinem Fall liegt das Problem also überhaupt nicht an der Stelle, an der
du den Fehler bekommst, sondern irgendwo vorher, wo du Daten in Bytes
einliest, ohne sie zu dekodieren. Python 3 macht den Programmfehler
offensichtlich, den Python 2 früher so halbwegs unter den Teppich gekehrt
hat (und der dadurch öfter mal nach Jahren noch zu unschönen Überraschungen
geführt hat).

Stefan

Back to de.comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

python2 --> python3 : str vs bytes Ulli Horlacher <framstag@rus.uni-stuttgart.de> - 2019-08-25 08:00 +0000
  Re: python2 --> python3 : str vs bytes Hermann Riemann <nospam.ng@hermann-riemann.de> - 2019-08-25 10:20 +0200
  Re: [Python-de] python2 --> python3 : str vs bytes Stefan Behnel <python-de@behnel.de> - 2019-08-25 10:44 +0200
    Re: [Python-de] python2 --> python3 : str vs bytes Hermann Riemann <nospam.ng@hermann-riemann.de> - 2019-08-26 07:03 +0200

csiph-web