Seite 1 von 1

Warum läuft nicht?

Verfasst: Donnerstag 4. Oktober 2018, 06:28
von nuhakan
Hallo,

ich bin wieder mit Learn Python the Hard Way. In einer Übung bekomme ich einen Fehler und finde leider die Lösung nicht.

Code: Alles auswählen

$ python3 ex23.py utf-8 strict
b'Afrikaans' <===> Afrikaans
b'\xc3\xa1\xc5\xa0 \xc3\xa1\xcb\x86\xe2\x80\xba\xc3\xa1\xcb\x86\xc2\xad\xc3\xa1\xc5\xa0\xe2\x80\xba' <===> Traceback (most recent call last):
  File "ex23.py", line 23, in <module>
    main(languages, input_encoding, error)
  File "ex23.py", line 10, in main
    return main(language_file, encoding, errors)
  File "ex23.py", line 9, in main
    print_line(line, encoding, errors)
  File "ex23.py", line 18, in print_line
    print(raw_bytes, "<===>", cooked_string)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128)

Code: Alles auswählen

import sys
script, input_encoding, error = sys.argv


def main(language_file, encoding, errors):
    line = language_file.readline()

    if line:
        print_line(line, encoding, errors)
        return main(language_file, encoding, errors)


def print_line(line, encoding, errors):
    next_lang = line.strip()
    raw_bytes = next_lang.encode(encoding, errors=errors)
    cooked_string = raw_bytes.decode(encoding, errors=errors)

    print(raw_bytes, "<===>", cooked_string)


languages = open("languages.txt", encoding="utf-8")

main(languages, input_encoding, error)
UTF-8 habe ich.

Code: Alles auswählen

$ locale
locale: Cannot set LC_CTYPE to default locale: No such file or directory
locale: Cannot set LC_MESSAGES to default locale: No such file or directory
locale: Cannot set LC_ALL to default locale: No such file or directory
LANG=en_GB.UTF-8
LC_CTYPE="en_GB.UTF-8"
LC_NUMERIC="en_GB.UTF-8"
LC_TIME="en_GB.UTF-8"
LC_COLLATE="en_GB.UTF-8"
LC_MONETARY="en_GB.UTF-8"
LC_MESSAGES="en_GB.UTF-8"
LC_PAPER="en_GB.UTF-8"
LC_NAME="en_GB.UTF-8"
LC_ADDRESS="en_GB.UTF-8"
LC_TELEPHONE="en_GB.UTF-8"
LC_MEASUREMENT="en_GB.UTF-8"
LC_IDENTIFICATION="en_GB.UTF-8"
LC_ALL=
Grüße.

Re: Warum läuft nicht?

Verfasst: Donnerstag 4. Oktober 2018, 08:28
von Sirius3
Rekursion bei 'main' ist die falsche Vorgehensweise. Nimm eine for-Schleife. Warum öffnest Du die Datei als 'utf-8' obwohl Du doch ein Encoding angibst? Das Dekodieren eines eben Encodierten Strings ist überflüssig, weil da nur wieder der Ursprüngliche String herauskommen kann. Dateien, die man öffnet sollte man auch wieder schließen:

Code: Alles auswählen

import sys

def print_line(line, encoding, errors):
    raw_bytes = line.encode(encoding, errors=errors)
    print(raw_bytes, "<===>", line)

def main():
    _, input_encoding, error = sys.argv
    with open("languages.txt", encoding="utf-8") as lines:
        for line in lines:
            print_line(line.strip(), encoding, errors)

if __name__ == '__main__':
    main()
Zum Problem: Dein System kennt kein `en_GB.UTF-8`. Welche Locale hast Du denn installiert?

Re: Warum läuft nicht?

Verfasst: Donnerstag 4. Oktober 2018, 14:14
von nuhakan
Hallo @Sirius3,

es sieht so aus, dass mein Problem UTF-8 war. In einen Derivat von Arch Linux hatte ich en_GB.UTF-8 als Local, aber bei /etc/locale.gen en_US.UTF-8 aktiviert. Oder etwas ähnliches. :D

Die Übung ist einfach so. Ich schreibe ab was im Buch steht. Ehrlich gesagt, verstehe nicht ganz was der Autor zeigen will und bin mit der Erklärung nicht schlau.

Danke!!

Re: Warum läuft nicht?

Verfasst: Donnerstag 4. Oktober 2018, 15:12
von __blackjack__
Also wenn dieser rekursive `main()`-Aufruf im Buch steht, würde ich das ja verbrennen. :-)

Re: Warum läuft nicht?

Verfasst: Donnerstag 4. Oktober 2018, 15:28
von sls
__blackjack__ hat geschrieben: Donnerstag 4. Oktober 2018, 15:12 Also wenn dieser rekursive `main()`-Aufruf im Buch steht, würde ich das ja verbrennen. :-)
Da möchte ich den Autor gerne zitieren:
I’ve been programming for a very long time—so long that it’s incredibly boring to me. At the time that I wrote this book, I knew about 20 programming languages and could learn new ones in about a day to a week, depending on how weird they were.
https://www.oreilly.com/library/view/le ... /ch54.html

Gutes Beispiel dafür dass die 10k Stunden alleine wohl nicht reichen :-)

Re: Warum läuft nicht?

Verfasst: Donnerstag 4. Oktober 2018, 15:52
von __blackjack__
Sooo schlechten Code hatte ich von Herrn Shaw gar nicht in Erinnerung. Das Buch war ja mal frei verfügbar.

Re: Warum läuft nicht?

Verfasst: Donnerstag 4. Oktober 2018, 19:23
von nuhakan
Ich kann den Code nicht beurteilen, aber nicht vergessen, es ist eine Übung um zu lernen. Der Autor mag solche trickst, damit den Lehrling nachdenkt.
10 I have written a tiny yet powerful piece of magic here. I am calling main again inside main. Actually it’s not magic since nothing really is magical in programming. All the information you need is there. This looks like I am calling the function inside itself, which seems like it should be illegal to do. Ask yourself, why should that be illegal? There’s no technical reason why I can’t call any function I want right there, even this main function. If a function is simply a jump to the top where I’ve named it main, then calling this function at the end of itself would... jump back to the top and run it again. That would make it loop. Now look back at line 8, and you’ll see the if-statement keeps this function from looping forever. Carefully study this because it is a significant concept, but don’t worry if you can’t grasp it right away.
23 The end of the script simply runs the main function with all the correct parameters to get everything going and kick-start the loop. Remember that this then jumps to where the main function is defined on line 5, and on line 10 main is called again, causing this to keep looping. The if line: on line 8 will prevent our loop from going forever.

Versteht man besser den Code bzw. die Übung? Ich habe mich für einen neuen Anfang entschieden und dieses Buch gewählt. Dies hat einen guten Ruf und wurde hier mehrere Mal empfohlen.

Eigentlich habe ich jetzt mehr gelernt als ich vorhatte. Danke für die Kommentare.

Re: Warum läuft nicht?

Verfasst: Donnerstag 4. Oktober 2018, 19:43
von Sirius3
If a function is simply a jump to the top where I’ve named it main, ...
Diese Aussage stimmt nicht. Warum der Autor hier ohne Not Rekursion einsetzt und falsch erklärt, wird wohl sein Geheimnis bleiben.

Re: Warum läuft nicht?

Verfasst: Donnerstag 4. Oktober 2018, 19:50
von __blackjack__
Vor allem ist das nicht nur unschön sondern wird auch irgendwann das Programm zum Absturz bringen. Damit würde ich das direkt mal als Programmierfehler ansehen.

Re: Warum läuft nicht?

Verfasst: Donnerstag 4. Oktober 2018, 20:26
von sls
__blackjack__ hat geschrieben: Donnerstag 4. Oktober 2018, 15:52 Sooo schlechten Code hatte ich von Herrn Shaw gar nicht in Erinnerung. Das Buch war ja mal frei verfügbar.
Na, dann hast du seine Lektüren ja gründlich studiert :-)

Vielleicht hing ihm einfach nur zum Hals raus, dass jeder Rekursion mit der fibonacci sequence erklären muss. Oder seine Arroganz hat ihn geblendet - Python ist ja nur eine einfache Script-Sprache. Ein Tag, dann kann man die.

Re: Warum läuft nicht?

Verfasst: Donnerstag 4. Oktober 2018, 20:43
von nuhakan
Oh je! Bisher habe ich das Buch gut gefunden. Nur dieses Script ist ein bisschen komisch. Vielleicht gibt es eine bessere Erklärung in nächsten Übungen.

Es ist nicht mehr das Thema, aber soll ich mir überlegen anderes Buch? Meine Liebe zu Python geht irgendwie schief. :S