Cannot print greek letters in Python 2.6

Steven D'Aprano steve+comp.lang.python at pearwood.info
Thu Nov 28 22:48:23 EST 2013


On Thu, 28 Nov 2013 14:22:01 -0800, mefistofelis wrote:

> I have the following script however when the clipboard contents are
> greek letters it fails to print them right. I have used all posible
> encoding for greek letters including utf8 but to no avail so i just stay
> with latin1.
> 
> Can you suggest a solution?
> 
> Here is the script:
> 
> import win32clipboard
> win32clipboard.OpenClipboard()
> data = win32clipboard.GetClipboardData()
> win32clipboard.CloseClipboard()
> data = data.decode('latin1')

That cannot possibly work, since there are no Greek letters in Latin1. If 
you run this piece of code, you will see no Greek letters except for µ 
MICRO SIGN.


import unicodedata
for i in range(256):
    c = chr(i).decode('latin-1')
    print c, unicodedata.name(c, "<no name>")


I'm not an expert on Windows, but my guess is that the data coming out of 
the clipboard could be using one of these encodings:

ISO-8859-7  # The code page used by some Greek versions of Windows.
UTF-16be
UTF-16
UTF-8


I'd try ISO-8859-7 and UTF-16be first, like this:

import win32clipboard
win32clipboard.OpenClipboard()
data = win32clipboard.GetClipboardData()
win32clipboard.CloseClipboard()
data = data.decode('UTF-16BE')



-- 
Steven



More information about the Python-list mailing list