473,472 Members | 1,760 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

how to print the GREEK CAPITAL LETTER delta under utf-8 encoding

I lookup the utf-8 form of delta from the link.
http://www.fileformat.info/info/unic...0394/index.htm

and then I want to print it in the python ( I work under windows)

#!/usr/bin/python
#coding=utf-8

print "\xce\x94"

but the result is not the 'delta' but an unknown character.

May 29 '07 #1
8 12412
人言落日是天涯,望极天涯不见家 schrieb:
I lookup the utf-8 form of delta from the link.
http://www.fileformat.info/info/unic...0394/index.htm

and then I want to print it in the python ( I work under windows)

#!/usr/bin/python
#coding=utf-8

print "\xce\x94"

but the result is not the 'delta' but an unknown character.
I assume you print to the terminal (cmd.exe). This cannot work;
the terminal (usually) does not interpret the characters in UTF-8.
Instead, you should print a Unicode string, e.g.

print u"\N{GREEK CAPITAL LETTER DELTA}"

or

print u'\u0394'

This should work as long as your terminal supports printing
the letter at all.

Regards,
Martin
May 29 '07 #2
On 5月29日, 下午1时34分, "Martin v. Lo"wis" <mar...@v.loewis.dewrote:
人言落日是天涯,望极天涯不见家 schrieb:
I lookup the utf-8 form of delta from the link.
http://www.fileformat.info/info/unic...0394/index.htm
and then I want to print it in the python ( I work under windows)
#!/usr/bin/python
#coding=utf-8
print "\xce\x94"
but the result is not the 'delta' but an unknown character.

I assume you print to the terminal (cmd.exe). This cannot work;
the terminal (usually) does not interpret the characters in UTF-8.
Instead, you should print a Unicode string, e.g.

print u"\N{GREEK CAPITAL LETTER DELTA}"

or

print u'\u0394'

This should work as long as your terminal supports printing
the letter at all.

Regards,
Martin
yes, it could print to the terminal(cmd.exe), but when I write these
string to file. I got the follow error:

File "E:\Tools\filegen\filegen.py", line 212, in write
self.file.write(data)
UnicodeEncodeError: 'ascii' codec can't encode character u'\u0394' in
position 0
: ordinal not in range(128)

but other text, in which include "chinese characters" got from
os.listdir(...), are written to the file OK. why?

May 29 '07 #3
yes, it could print to the terminal(cmd.exe), but when I write these
string to file. I got the follow error:

File "E:\Tools\filegen\filegen.py", line 212, in write
self.file.write(data)
UnicodeEncodeError: 'ascii' codec can't encode character u'\u0394' in
position 0
: ordinal not in range(128)
Yes, when writing to a file, you need to define an encoding, e.g.

self.file.write(data.encode("utf-8"))

You can use codecs.open() instead of open(),
so that you can just use self.file.write(data)

Alternatively, you can find out what sys.stdout.encoding is,
and use that when encoding data for the terminal (falling back
to "utf-8" when .encoding is not available on the file).
but other text, in which include "chinese characters" got from
os.listdir(...), are written to the file OK. why?
Your version of Windows uses a code page that supports Chinese
characters in the byte-oriented character set. These are normally
encoded using the "mbcs" encoding (except that the terminal likely
uses a different encoding). So if you use "mbcs" instead of "utf-8",
you might be able to read the text as well.

Regards,
Martin
May 29 '07 #4
On 5月29日, 下午3时05分, "Martin v. Lo"wis" <mar...@v.loewis.dewrote:
yes, it could print to the terminal(cmd.exe), but when I write these
string to file. I got the follow error:
File "E:\Tools\filegen\filegen.py", line 212, in write
self.file.write(data)
UnicodeEncodeError: 'ascii' codec can't encode character u'\u0394' in
position 0
: ordinal not in range(128)

Yes, when writing to a file, you need to define an encoding, e.g.

self.file.write(data.encode("utf-8"))

You can use codecs.open() instead of open(),
so that you can just use self.file.write(data)

Alternatively, you can find out what sys.stdout.encoding is,
and use that when encoding data for the terminal (falling back
to "utf-8" when .encoding is not available on the file).
but other text, in which include "chinese characters" got from
os.listdir(...), are written to the file OK. why?

Your version of Windows uses a code page that supports Chinese
characters in the byte-oriented character set. These are normally
encoded using the "mbcs" encoding (except that the terminal likely
uses a different encoding). So if you use "mbcs" instead of "utf-8",
you might be able to read the text as well.

Regards,
Martin
Thanks a lot!
I want to just use the utf-8. how could I convert my 'mbcs' encoding
to the utf-8 and write it to the file?
I have replaced the open() to codecs.open()

but it still can not decode the 'mbcs', the error is as follow:

File "E:\Tools\filegen\filegen.py", line 213, in write
self.file.write(data)
File "C:\Python25\lib\codecs.py", line 638, in write
return self.writer.write(data)
File "C:\Python25\lib\codecs.py", line 303, in write
data, consumed = self.encode(object, self.errors)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xcc in position
32: ordinal
not in range(128)

May 29 '07 #5
En Tue, 29 May 2007 04:24:15 -0300, 浜鸿█钀芥棩鏄ぉ娑紝鏈涙瀬澶╂动涓嶈瀹
<ke********@gmail.comescribi贸:
On 5鏈29鏃, 涓嬪崍3鏃05鍒, "Martin v. Lo"wis" <mar...@v.loewis.dewrote:
yes, it could print to the terminal(cmd.exe), but when I write these
string to file. I got the follow error:
File "E:\Tools\filegen\filegen.py", line 212, in write
self.file.write(data)
UnicodeEncodeError: 'ascii' codec can't encode character u'\u0394' in
position 0
: ordinal not in range(128)

Yes, when writing to a file, you need to define an encoding, e.g.

self.file.write(data.encode("utf-8"))

You can use codecs.open() instead of open(),
so that you can just use self.file.write(data)
Thanks a lot!
I want to just use the utf-8. how could I convert my 'mbcs' encoding
to the utf-8 and write it to the file?
I have replaced the open() to codecs.open()

but it still can not decode the 'mbcs', the error is as follow:

File "E:\Tools\filegen\filegen.py", line 213, in write
self.file.write(data)
File "C:\Python25\lib\codecs.py", line 638, in write
return self.writer.write(data)
File "C:\Python25\lib\codecs.py", line 303, in write
data, consumed = self.encode(object, self.errors)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xcc in position
32: ordinal
not in range(128)
Just to provide an example of what MvL already said:

pyline = u"Delta=\u0394"
pyf = open("data.txt","w")
pyf.write(line.encode("utf8"))
pyf.close()
pyprint repr(open("data.txt").read())
'Delta=\xce\x94'

pyimport codecs
pyf = codecs.open("data2.txt","w","utf8")
pyf.write(line)
pyf.close()
pyprint repr(open("data2.txt").read())
'Delta=\xce\x94'

--
Gabriel Genellina

May 29 '07 #6
tis 2007-05-29 klockan 09:05 +0200 skrev "Martin v. Lo"wis":
Yes, when writing to a file, you need to define an encoding, e.g.

self.file.write(data.encode("utf-8"))

You can use codecs.open() instead of open(),
so that you can just use self.file.write(data)
If I for some reason can't open the object myself or needs encoding on
other file-like objects, I think the following wrapper function is of
use (it essentially does what codecs.open() does but takes a file-object
instead of a filename):

def filewrapper(f, encoding=None, errors='strict'):
if encoding is None:
return f

info = codecs.lookup(encoding)
srw = codecs.StreamReaderWriter(f, info.streamreader,
info.streamwriter, errors)
# Add attributes to simplify introspection
srw.encoding = encoding
return srw

I find this especially useful for changing how stdout and friends does
it's encoding, e.g:
>>sys.stdout = filewrapper(sys.stdout, 'utf-8')
print u"邃 \N{GREEK CAPITAL LETTER DELTA}"
Useful if you don't want to append .encode() to everything you print out
that potentially can contain non-ascii letters.

/Ragnar

May 29 '07 #7
En Tue, 29 May 2007 15:16:52 -0300, Ragnar Ouchterlony
<ra****@lysator.liu.seescribi:
If I for some reason can't open the object myself or needs encoding on
other file-like objects, I think the following wrapper function is of
use (it essentially does what codecs.open() does but takes a file-object
instead of a filename):

def filewrapper(f, encoding=None, errors='strict'):
if encoding is None:
return f

info = codecs.lookup(encoding)
srw = codecs.StreamReaderWriter(f, info.streamreader,
info.streamwriter, errors)
# Add attributes to simplify introspection
srw.encoding = encoding
return srw

I find this especially useful for changing how stdout and friends does
it's encoding, e.g:
>>>sys.stdout = filewrapper(sys.stdout, 'utf-8')
print u"邃 \N{GREEK CAPITAL LETTER DELTA}"

Useful if you don't want to append .encode() to everything you print out
that potentially can contain non-ascii letters.
Isn't the same as codecs.EncodedFile?

--
Gabriel Genellina

May 29 '07 #8
tis 2007-05-29 klockan 16:08 -0300 skrev Gabriel Genellina:
>>sys.stdout = filewrapper(sys.stdout, 'utf-8')
print u"邃 \N{GREEK CAPITAL LETTER DELTA}"
Useful if you don't want to append .encode() to everything you print out
that potentially can contain non-ascii letters.
Isn't the same as codecs.EncodedFile?
No, codecs.EncodedFile() doesn't do exactly the same.

If I understand it correctly, EncodedFile() takes a byte stream as input
and produces another bytestream (with possibly different encoding) as
output in both read and write.

My function (as well as codecs.open()) decodes a byte stream for read
and produces a unicode object or encodes a unicode object for write and
produces a byte stream.

At least, I were unable to create the same behaviour as my filewrapper()
using EncodedFile(). If you are able to do that, I'm interested in how
you do it.

/Ragnar

May 29 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Luke | last post by:
Python doesn't seem to read UTF-8 properly from an interactive session. Am I doing something wrong? luked@sor ~ $ echo $LANG en_AU.UTF-8 luked@sor ~ $ python Python 2.3.4 (#1, Aug 12 2004,...
7
by: Barry | last post by:
Hi all, I've noticed a strange error on my website. When I print a capital letter P with a dot above, using & #7766; it appears correctly, but when I use P& #0775 it doesn't. The following...
1
by: Marc Verdonck | last post by:
First: I have some troubles in access, some fields needs to begin with a Capital letter. Example: louiselaan need to be Louiselaan. How can I become this? Second: I'm exporting my database to...
5
by: Agnes | last post by:
In textbox, there is a property (character casing), However, in combox , any method/property to set it (as the user input the text, it is default to upper case ? Thanks a lot
2
by: Gary Wessle | last post by:
I am going through this tut from http://ibiblio.org/obp/thinkCS/python/english/chap07.htm I am getting errors running those 2 groups as below as is from the tut thanks index = 0
22
by: hg | last post by:
Hi, I'm bringing over a thread that's going on on f.c.l.python. The point was to get rid of french accents from words. We noticed that len('') != len('a') and I found the hack below to fix...
0
by: =?Utf-8?B?Sm9hbiBNYWM=?= | last post by:
Trying to make signs for demonstrations and I need to print individual letters 4 inches high. Thought I used to do it Word (Drawing tool) before I got Vista and the horror they have made out of...
5
by: Moham12345 | last post by:
How can i detect if there is more then capital letter in a word?
1
by: mujunshan | last post by:
Yesterday, I installed PythonCE on my cellphone whose OS is Windows Mobile 5.I wanted to use numpy as calculation tool.But after I copy numpy module in my desktop computer into my phone,I find many...
4
by: zcabeli | last post by:
Hi all, i'm currently struggling to perform the above mentioned replacement. i already know how to catch the first letter in each word and determined if it's regular or capital. however, i...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project梡lanning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.