G+_Guttormur Ingi Einarsso Posted April 18, 2014 Share Posted April 18, 2014 Hi I'm working on a project to try to understand most of the twit coding 101 is teaching but there is one issue that I'm pretty sure they will not cover and was hoping that some one here could help me with, I'm having players of a CLI based game enter their name but we here in Iceland have all those weird letters that python is not happy with, like Þ ó é í ö æ ð, I'm pretty sure that this is because Python is not using utf8 by default but I cant find a way to deal with this on the google. can some one help or point me in the right direction ? Link to comment Share on other sites More sharing options...
G+_Guttormur Ingi Einarsso Posted April 18, 2014 Author Share Posted April 18, 2014 a small test in idle name = raw_input('enter name ') enter name þrúða >>> name '\xfer\xfa\xf0a' Link to comment Share on other sites More sharing options...
G+_Tadeusz Cantwell Posted April 18, 2014 Share Posted April 18, 2014 try >>> print name Link to comment Share on other sites More sharing options...
G+_Guttormur Ingi Einarsso Posted April 18, 2014 Author Share Posted April 18, 2014 thanks that helps with íóéö but þ and ð are still a problem as I can not enter them while ruining a .py script in 2.7 but works fine in 3.0 Link to comment Share on other sites More sharing options...
G+_Tadeusz Cantwell Posted April 18, 2014 Share Posted April 18, 2014 You can import all uf8 characters with the second line. Should work for all languages. #! /usr/bin/env python # -*- coding: utf-8 -*- Link to comment Share on other sites More sharing options...
G+_Lee Crocker Posted April 18, 2014 Share Posted April 18, 2014 Python 2.7 has a different data type for Unicode strings. I'd recommend you start with Python 3, which has better Unicode integration (all strings are Unicode by default, and source is assumed to be utf8). The only thing you'll need to change from Padre's examples is to make sure you call print() as a function. BTW, you can install both versions of Python on one computer and they will politely stay out of each others way. Link to comment Share on other sites More sharing options...
G+_Lee Crocker Posted April 18, 2014 Share Posted April 18, 2014 ... Oh, and Python 3 uses ”input" instead of raw_input. Link to comment Share on other sites More sharing options...
G+_Guttormur Ingi Einarsso Posted April 19, 2014 Author Share Posted April 19, 2014 Thanks for the info I migrated the project to python 3.0, but its still giving my issues, I'm testing both on windows 8.1 and Debian on the windows box works but I can't type in the þ and ð and the Linux one just crashes on all special chars... but allows me to type all of them Link to comment Share on other sites More sharing options...
G+_Tadeusz Cantwell Posted April 19, 2014 Share Posted April 19, 2014 Can you post the error message. I tried the uf8 line with python 2, on ubuntu and it worked fine. Link to comment Share on other sites More sharing options...
G+_Guttormur Ingi Einarsso Posted April 19, 2014 Author Share Posted April 19, 2014 enter name Markús Traceback (most recent call last): File "./kot-class.py", line 98, in name = input("enter name ") UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 4: ordinal not in range(128) Link to comment Share on other sites More sharing options...
G+_Guttormur Ingi Einarsso Posted April 19, 2014 Author Share Posted April 19, 2014 this is from py 3.4 Link to comment Share on other sites More sharing options...
G+_Lee Crocker Posted April 19, 2014 Share Posted April 19, 2014 Strange, I have no trouble on Debian. This is a simple transcript of a command line session in another window: lee@aces:~$ python3 Python 3.3.5 (default, Mar 22 2014, 13:24:53)[GCC 4.8.2] on linux Type "help", "copyright", "credits" or "license" for more information. >>> s = input("String:") String:aböcédþfðg >>> print(s) aböcédþfðg >>> Of course I had to copy the thorn and edh in from charmap since there's no compose-key sequence for them, but I assume you have an Icelandic keyboard so that's not an issue. Perhaps your system just isn't configured correctly for your keyboard? Link to comment Share on other sites More sharing options...
G+_Tadeusz Cantwell Posted April 19, 2014 Share Posted April 19, 2014 Just got the alt codes for the troublesome characters. Will try typing instead of copying. alt 0254 => þ alt 0240 => ð EDIT: The alt codes work for windows. shift ctrl u + unicode should work on ubuntu. 00DE 00FE works for lower+ uppercase thorn U+00D0 U+00F0 isn't putting the character on screen. Anyway both thorn's print fine for me. Will have to play with it more. Link to comment Share on other sites More sharing options...
Recommended Posts