473,545 Members | 2,451 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem with national characters

I'm developing a routine that will parse user input. For simplicity, I'm
converting the entire input string to upper case. One of the words that
will have special meaning for the parser is the word "før", (before in
English). However, this word is not recognized. A test in the
interactive shell reveals this:

leif@balapapa leif $ python
Python 2.3.4 (#1, Feb 7 2005, 21:31:38)
[GCC 3.3.5 (Gentoo Linux 3.3.5-r1, ssp-3.3.2-3, pie-8.7.7.1)] on linux2
Type "help", "copyright" , "credits" or "license" for more information.
'før'.upper() 'F\xf8R' 'FØR' 'F\xd8R'
In Windows, the result is slightly different, but no better:

C:\Python23>pyt hon
ActivePython 2.3.2 Build 232 (ActiveState Corp.) based on
Python 2.3.2 (#49, Nov 13 2003, 10:34:54) [MSC v.1200 32 bit (Intel)] on
win32
Type "help", "copyright" , "credits" or "license" for more information. 'før'.upper() 'F\x9bR' 'FØR' 'F\x9dR'


Is there a way around this problem? My character set in Linux is
ISO-8859-1. In Windows 2000 it should be the equivavent Latin-1, though
I'm not sure about which character set the command shell is using.
--
Leif Biberg Kristensen
http://solumslekt.org/
Jul 18 '05 #1
7 2558
Is there a way around this problem?


put

import sys
sys.setdefaulte ncoding('UTF-8')

into sitecustomize.p y in the top level of your PYTHONPATH .

Jul 18 '05 #2
da**********@gm ail.com wrote:
put

import sys
sys.setdefaulte ncoding('UTF-8')

into sitecustomize.p y in the top level of your PYTHONPATH .


Uh ... it doesn't seem like I've got PYTHONPATH defined on my system in
the first place:

leif@balapapa leif $ env |grep -i python
PYTHONDOCS=/usr/share/doc/python-docs-2.3.4/html

I ran this small snippet that I found after a search on Gentoo-forums:
import sys
import string
path_list = sys.path
for eachPath in path_list: print eachPath ....

/usr/lib/python23.zip
/usr/lib/python2.3
/usr/lib/python2.3/plat-linux2
/usr/lib/python2.3/lib-tk
/usr/lib/python2.3/lib-dynload
/usr/lib/portage/pym
/usr/lib/python2.3/site-packages
/usr/lib/python2.3/site-packages/gtk-2.0


What should my PYTHONPATH look like, and where do you suggest to put the
sitecustomize.p y file?
--
Leif Biberg Kristensen
http://solumslekt.org/
Jul 18 '05 #3
> da**********@gm ail.com wrote:
put

import sys
sys.setdefaulte ncoding('UTF-8')

into sitecustomize.p y in the top level of your PYTHONPATH .


I found out of it, sort of. Now I've got a PYTHONPATH that points to my
home directory, and followed your instructions. The first time I got an
error message due to a typo. I corrected it, and now Python starts
without an error message. But it didn't solve my problem with the
uppercase Ø at all. Is there something else I have to do?
--
Leif Biberg Kristensen
http://solumslekt.org/
Jul 18 '05 #4
Leif B. Kristensen skrev:
Is there something else I have to do?


Please forgive me for talking with myself here :-) I should have looked
up Unicode in "Learning Python" before I asked. This seems to work:
u'før'.upper() u'F\xd8R' u'FØR' u'F\xd8R' 'FØR' 'F\xd8R'

So far, so good. Note that the Unicode representation of the uppercase
version is identical to the default. But when I try the builtin
function unicode(), weird things happen:
s='FØR'
s 'F\xd8R' unicode(s)

Traceback (most recent call last):
File "<stdin>", line 1, in ?
UnicodeDecodeEr ror: 'utf8' codec can't decode bytes in position 1-2:
invalid data

The ActivePython 2.3.2 doesn't even seem to understand the 'u' prefix.
So even if I can get this to work on my own Linux machine, it hardly
looks like a portable solution.

Seems like the "solution" is to keep away from letters above ASCII-127,
like we've done since the dawn of computing ...
--
Leif Biberg Kristensen
http://solumslekt.org/
Jul 18 '05 #5
Leif B. Kristensen wrote:
Is there a way around this problem? My character set in Linux is
ISO-8859-1. In Windows 2000 it should be the equivavent Latin-1, though
I'm not sure about which character set the command shell is using.


The unicode methods seems to do it correctly. So you can decode your
strings as unicode, do the transfom, and encode it back as latin1.

print repr('før'.deco de('latin-1').upper().enc ode('latin-1')) #
'F\xd8R'
print repr('FØR'.deco de('latin-1').encode('lat in-1'))
'F\xd8R'

--

hilsen/regards Max M, Denmark

http://www.mxm.dk/
IT's Mad Science
Jul 18 '05 #6
Leif B. Kristensen wrote:
Is there a way around this problem? My character set in Linux is
ISO-8859-1. In Windows 2000 it should be the equivavent Latin-1, though
I'm not sure about which character set the command shell is using.


You need to do locale.setlocal e(locale.LC_ALL , "") to get
locale-specific upper-casing.

Notice that things are more difficult in the Windows terminal window,
as this uses an encoding different from the one that the system's
locale functions expect.

Regards,
Martin
Jul 18 '05 #7
"Martin v. Löwis" skrev:
You need to do locale.setlocal e(locale.LC_ALL , "") to get
locale-specific upper-casing.
That makes a lot of sense. Thank you.
'før'.upper() 'F\xf8R' 'FØR' 'F\xd8R' import locale
locale.setlocal e(locale.LC_ALL , "") 'no_NO' 'før'.upper() 'F\xd8R' 'FØR'

'F\xd8R'

I must make a note of the LC_ALL variable in the installation README.
I for one have been running Gentoo Linux for two years without ever
setting the locale, - but now I've finally gotten around to write my
own /etc/env.d/02locale file :-)
Notice that things are more difficult in the Windows terminal window,
as this uses an encoding different from the one that the system's
locale functions expect.


The real input will come from a GUI or a browser interface, so the
Windows terminal problem isn't really an issue.
--
Leif Biberg Kristensen
http://solumslekt.org/
Jul 18 '05 #8

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

Similar topics

2
1179
by: Jacek Stepniewski | last post by:
I would like to display on the cell phone my regional characters (polish characters). The best way to do this is to use entity, but the mobile control do HtmlEncode and converts "&" sign to "&amp". For example: - if i do lblTest.Text = "&#x104", the resoult is: &amp;#x104; So this way is wrong. Is there any way using moblie controls to display...
0
1074
by: Peter Hemmingsen | last post by:
Hi, I've an asp.net (cs code behind) application where I want the user to be able to download a file with national (danish) characters. Ex: Response.Clear(); // clear the current output content from the buffer Response.AddHeader("Content-Disposition", "attachment; filename=" +
0
1270
by: Eric Carr | last post by:
Hi, we have been using WMI from VB6 to automate configuration of new DNS zones on our win2000 servers, and are now trying to move the system to a vb.net application. We recently added some zones with names containing national characters (norwegian ÆØÅ), and then everything stopped working, including the old VB6 app. The error occurs when...
4
2552
by: B.D. | last post by:
Can anyone explain way the transformation to upper case doesn't work correctly in the following code if PROBLEM is defined but works correctly if it's not defined? I'm using VC 7.1 #include <locale> #include <string> #include <algorithm>
3
1518
by: ianeruda | last post by:
Hi, I created database using CREATE DATABASE TEST ON D: USING CODESET 1250 TERRITORY HR COLLATE USING SYSTEM in order to sort national specific characters in right order. But this still doesn't work. Characters are inserted and displayed correctly, but the sort order is wrong.
0
991
by: wolf2300 | last post by:
readline method and national charset problem hi how can i copy line from text file which contains polish characters. i use readline method from StreamReader class but it doesn't copy polish characters :( what shall i do ?? this is a code how can i change it to works the same way
3
1987
by: Dariusz Tomon | last post by:
Hi My problem is like that: I'm trying to pass values branza and jezyk in querysting: http://localhost/euroadres/pokazbranza.aspx?branza=transport lotniczy&jezyk=1 Branza is type "string" and when I'm passing string value without national
1
3739
by: skyson2ye | last post by:
Hi, guys: I have written a piece of code which utilizes Javascript in PHP to create a three level dynamic list box(Country, States/Province, Market). However, I have encountered a strange problem, and I have spent three days trying to debug but to no avail. Everything is OK when there are only two dependent list boxes, but when adding the...
2
3001
by: swethak | last post by:
hi , i write the code in .htm file. It is in cgi-bin/searches/one.htm.In that i write a form submitting and validations.But validations are not worked in that .htm file. I used the same code in my local system that validations work.plz tell that whats the problem in that. Here is my code <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0...
0
7420
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7446
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5349
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4966
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3476
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3459
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1908
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 we have to send another system
1
1033
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
731
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.