473,387 Members | 1,318 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How do I display unicode-paths?

I've been trying to stay blissfully unaware of Unicode, however now it seems like it's my turn. From the outside it seems like a rather massive subject, so any pointers as to where I should _start_ reading would be appreciated. The usecase is a class:

class path(object):
...
def __str__(self):
return self.pathstr.encode(???)

the question is what to put at ??? to be most useful to programmers/end users?

Background: I've got a directory with a file called 'b', B-AE, (which everyone knows is what Norwegian sheep say :-). If I type u'b' at the Python command prompt, it returns:

u'b\x91'

(WinXP, 2.3.2, regular command window). With a little trial and error I found:
print u'b\x91'.encode('latin1') b

although
print u'b\x91'

Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "e:\python23\lib\encodings\cp437.py", line 18, in encode
return codecs.charmap_encode(input,errors,encoding_map)
UnicodeEncodeError: 'charmap' codec can't encode character '\u91' in position 1:
character maps to <undefined>

not sure I'm understanding this, and when I call:

os.listdir(os.getcwdu())

I get back

u'b\xe6'

which isn't making much sense either...

help?!

-- bjorn

Jul 18 '05 #1
1 2242
Pettersen, Bjorn S wrote:
I've been trying to stay blissfully unaware of Unicode, however now it seems like it's my turn. From the outside it seems like a rather massive subject, so any pointers as to where I should _start_ reading would be appreciated. The usecase is a class:

class path(object):
...
def __str__(self):
return self.pathstr.encode(???)

the question is what to put at ??? to be most useful to programmers/end users?
It is not possible to put something "most useful" here. If you want to
convert a Unicode string to a byte string, you typically do so because
you need to put data into a byte-oriented channel (such as a file, a
socket, or a terminal). You *do* need to know the encoding of that
channel to convert the Unicode object. Unfortunately, __str__ has no way
of knowing where data will be put.

If you can assume that the bytes returned are displayed to the user on
the local system, you can try locale.getpreferredencoding(). This should
work in most cases, with the notable exception of a cmd.exe window on MS
Windows: the terminal uses an encoding *different* from the user's
preferred encoding, and it is impossible to know in advance what that
encoding will be.

So in short: Just don't convert the Unicode string into a byte string,
until you know exactly where it will be displayed.
Background: I've got a directory with a file called 'b', B-AE, (which everyone knows is what Norwegian sheep say :-). If I type u'b' at the Python command prompt, it returns:

u'b\x91'


Unfortunately, this did not work very well: Try the same in IDLE, and
you will get u'b\xe6'

Unicode character U+00E6 really is LATIN SMALL LETTER AE:
u"\N{LATIN SMALL LETTER AE}"

u'\xe6'

Unicode character U+0091 is *not* AE, it is some (useless) control
character. The problem is that the windows console was using MS CP850,
but Python was interpreting it as Latin-1 (see PEP 263).

Print u"\xe6" on the console should work fine, in Python 2.3 (since
Python finds out, at the time of putting out the bytes, what the
encoding of the console is).

Regards,
Martin

Jul 18 '05 #2

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

Similar topics

1
by: Posadas, Dennis | last post by:
Hi, I've done some google searching w/c referred me to a couple of articles on unicode, but I'm looking for something really simple - not for a web application. See my sample code below. Any...
5
by: Ali | last post by:
I was wondering how one would go about displaying unicode in a Label object in a Tkinter window. I am trying to display text in another language. Please help.
0
by: JJY | last post by:
Hi. I have a few sets of unicode strings I am trying to display. I can display session variables with unicode strings from a XML file, but I can't display a unicode string coming from a DLL. ...
76
by: Zenobia | last post by:
How do I display character 151 (long hyphen) in XHTML (utf-8) ? Is there another character that will substitute? The W3C validation parser, http://validator.w3.org, tells me that this character...
33
by: selowitch | last post by:
I've been searching in vain for a way to present typographically correct fractions (not resorting to <sup> and <sub> tags) but have been frustrated by the fact that the glyphs for one-half,...
3
by: Jennifer | last post by:
Hi, Is there anyone to have idea how to make the MFC CDialog box display Unicode (like Chinese characters) in .NET 2005? We have some dilog boxes created from CDialog class in VC++ 6.0 and try...
6
by: Bill Nguyen | last post by:
I'm getting data from a mySQL database (default char set = UTF-8). I need to display data in Unicode but got only mongolian characters like this: Phạm Thị Ngọc I changed the textbox font to...
6
by: Melissa | last post by:
Initially the form is loaded using ASP and HTML and the ü codes display the characters correctly. I have the values stored in a javascript array so that I can more easily and dynamically change...
6
by: coldpizza | last post by:
Hi! I am using the built-in Python web server (CGIHTTPServer) to serve pages via CGI. The problem I am having is that I get an error while trying to display Unicode UTF-8 characters via a...
1
by: jackbenimble999 | last post by:
Hello! What is the best way to display a EUC-encoded field with Access 2007? Or, failing that, how do you display a Unicode field as the character instead of the number? Do I need to use a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...

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.