This topic is READ ONLY
sandefan (39) [Avatar] Offline
#1
Hello,Warren,

in
http://www.manning-sandbox.com/thread.jspa?messageID=105770𙴪
you showed me how to get hangman (1st ed., with Pythoncard) to accept French or German (or other Western European) special characters, such as année or übung. This still works great, in python 2.7!

I am using Linux (Ubuntu 12.04, 64bit) and python 2.7.3. Now, from your 2nd edition of Hello World, I tested your listing_22-8.py. It works fine with Englisch letters in the file words.txt - the graphics are superior to pythoncard's. But - contrary to the pythoncard program - not with special characters beyond ASCII 127. This is how I changed your listing_22-8.py (exactly as you showed me for pythoncard):

added: import codecs

changed the 1st line in
#Get the word list
to: f=codecs.open("wordsDE.txt", 'r', encoding = 'utf-8')

Even adding # -*- coding: utf-8 -*- did not help: when I try to guess a word such as übung I get always this console message:

Traceback (most recent call last):
File "listing_22-8DE.py", line 79, in btn_guess_clicked
guess = str(self.guessBox.text())
UnicodeEncodeError: 'ascii' codec can't encode character u'xe4' in position 0: ordinal not in range(12smilie


How can I teach PyQt (is this the culprit?) to accept those characters?

Thank you,

siggi
sandefan (39) [Avatar] Offline
#2
Re: Hangman 2nd ed: how to handle French or German special characters?
Sorry, I forgot that I changed also the line in

# Replace letters with dashes at the start of the program
def dashes(word):
letters = "abcdefghijklmnopqrstuvwxyz"


to: letters = "abcdefghijklmnopqrstuvwxyzäöüß"
sandefan (39) [Avatar] Offline
#3
Re: Hangman 2nd ed: how to handle French or German special characters?
I don't know whether this helps, too, but anyway:

your program, and I mean your original program as posted online (not with coding modifications as posted above) works very well with special characters and with Python 3 except for two small modifications:

* 1 *
adding äöüß (if German), of course,as above: letters = "abcdefghijklmnopqrstuvwxyzäöüß"

* 2 *
Modify your def new_game(self): etc. to

def new_game(self):
self.guesses.setText("")
self.currentword = random.choice(self.lines) # Randomly pick a word from the list
self.currentword = self.currentword.strip()
while self.currentword == "":
self.currentword = random.choice(self.lines) # remove last
self.currentword = self.currentword.strip() # empty string

for i in self.pieces:

If you don't add the while loop, random.choice() sometimes picks the last empty string "", that cannot be guessed, at least not by me. E.g. see the console printout of a short file words.txt:

['water
', 'befürworten
', 'ähre
', 'öffnen
', '
']

with '
as the last item.

With these modifications your program works very well with non-English words (I have not tested French and other languages so far, let alone Chinese smilie but it should be ok).et

So, I'd better move to Great Britain, the United States or Australia when using Python2 smilie

Greetings,

siggi

Message was edited by:
sandefan
sandefan (39) [Avatar] Offline
#4
Re: Hangman 2nd ed: how to handle French or German special characters?
Problem solved for Python 2 and Hangman 2nd ed. (at least IMHO):

After having read this blog:
http://blog.notdot.net/2010/07/Getting-unicode-right-in-Python
I replaced every occurrence of str( with unicode(, that's it!

Again, here are the modifications I did to the original listing (and I am not sure whether you need all of them, but definitely you need the unicode( stuff) changes in bold:

1st or 2nd line:
# -*- coding: utf-8 -*-

import sys, codecs

def dashes(word):
letters = u"abcdefghijklmnopqrstuvwxyzäöüß"

#Get the word list
f=codecs.open('wordsDE.txt', 'r', encoding = 'utf-8')

replace every occurrence of "str(" with "unicode(", e.g.,
guess = unicode(self.guessBox.text());