473,321 Members | 1,708 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,321 software developers and data experts.

Unicode literals to latin-1

How can I convert a string read from a database containing unicode literals, such as "Fr\u00f8ya" to the latin-1 equivalent, "Frøya"?

I have tried variations around
"Fr\u00f8ya".decode('latin-1')
but to no avail.

..david
Jan 30 '08 #1
5 1189
>>>><Da***********@sweco.no(DR) wrote:
>DRHow can I convert a string read from a database containing unicode literals, such as "Fr\u00f8ya" to the latin-1 equivalent, "Frøya"?
DRI have tried variations around
DR "Fr\u00f8ya".decode('latin-1')
DRbut to no avail.
You have to use encode instead of decode, and the input string must be a
unicode string.
>>print u"Fr\u00f8ya".encode('latin-1')
Frøya
>>>

--
Piet van Oostrum <pi**@cs.uu.nl>
URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
Private email: pi**@vanoostrum.org
Jan 30 '08 #2
On Wed, 30 Jan 2008 09:57:55 +0100, David.Reksten wrote:
How can I convert a string read from a database containing unicode
literals, such as "Fr\u00f8ya" to the latin-1 equivalent, "Frøya"?

I have tried variations around
"Fr\u00f8ya".decode('latin-1')
but to no avail.
In [388]: 'Fr\u00f8ya'.decode('unicode-escape')
Out[388]: u'Fr\xf8ya'

In [389]: print 'Fr\u00f8ya'.decode('unicode-escape')
Frøya

Ciao,
Marc 'BlackJack' Rintsch
Jan 30 '08 #3
On 30. januar 2008 10:48, Marc 'BlackJack' Rintsch wrote:
>On Wed, 30 Jan 2008 09:57:55 +0100, David.Reksten wrote:
>How can I convert a string read from a database containing unicode
literals, such as "Fr\u00f8ya" to the latin-1 equivalent, "Frøya"?

I have tried variations around
"Fr\u00f8ya".decode('latin-1')
but to no avail.

In [388]: 'Fr\u00f8ya'.decode('unicode-escape')
Out[388]: u'Fr\xf8ya'

In [389]: print 'Fr\u00f8ya'.decode('unicode-escape')
Frøya
'unicode-escape' did the trick! Thank you!

..david
Jan 30 '08 #4
On 30 ene, 07:54, <David.Reks...@sweco.nowrote:
On 30. januar 2008 10:48, Marc 'BlackJack' Rintsch wrote:
On Wed, 30 Jan 2008 09:57:55 +0100, David.Reksten wrote:
How can I convert a string read from a database containing unicode
literals, such as "Fr\u00f8ya" to the latin-1 equivalent, "Frøya"?
In [388]: 'Fr\u00f8ya'.decode('unicode-escape')
Out[388]: u'Fr\xf8ya'

'unicode-escape' did the trick! Thank you!
A unicode-escaped string looks very strange in a database... I'd
revise the way things are stored and retrieved.

--
Gabriel Genellina
Jan 30 '08 #5
On 30. januar 2008 14:31, Gabriel Genellina wrote:
>On 30 ene, 07:54, <David.Reks...@sweco.nowrote:
>On 30. januar 2008 10:48, Marc 'BlackJack' Rintsch wrote:
>On Wed, 30 Jan 2008 09:57:55 +0100, David.Reksten wrote:
>How can I convert a string read from a database containing unicode
literals, such as "Fr\u00f8ya" to the latin-1 equivalent, "Frøya"?
>In [388]: 'Fr\u00f8ya'.decode('unicode-escape')
Out[388]: u'Fr\xf8ya'

'unicode-escape' did the trick! Thank you!

A unicode-escaped string looks very strange in a database... I'd
revise the way things are stored and retrieved.
I agree. I'm currently using the trick above to fix it.

..david
Jan 30 '08 #6

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

Similar topics

3
by: Gerhard Häring | last post by:
>>> u"äöü" u'\x84\x94\x81' (Python 2.2.3/2.3b2; sys.getdefaultencoding() == "ascii") Why does this work? Does Python guess which encoding I mean? I thought Python should refuse to guess...
11
by: Laurent Therond | last post by:
Maybe you have a minute to clarify the following matter... Consider: --- from cStringIO import StringIO def bencode_rec(x, b): t = type(x)
4
by: Guilherme Salgado | last post by:
Hi there, I have a python source file encoded in unicode(utf-8) with some iso8859-1 strings. I've encoded this file as utf-8 in the hope that python will understand these strings as unicode...
6
by: nico | last post by:
In my python scripts, I use a lot of accented characters as I work in french. In order to do this, I put the line # -*- coding: UTF-8 -*- at the beginning of the script file. Then, when I need...
14
by: wolfgang haefelinger | last post by:
Hi, I wonder whether someone could explain me a bit what's going on here: import sys # I'm running Mandrake 1o and Windows XP. print sys.version ## 2.3.3 (#2, Feb 17 2004, 11:45:40)
12
by: Onega | last post by:
Hi I create a simple win32 project (VC2003, windows2003(English) , and do simple paint in WM_PAINT message, when the project use multi-character set, it is OK. but when I change to UNICODE,...
15
by: John Salerno | last post by:
Forgive my newbieness, but I don't quite understand why Unicode is still something that needs special treatment in Python (and perhaps elsewhere). I'm reading Dive Into Python right now, and it...
14
by: abhi147 | last post by:
Hi , I want to convert an array of bytes like : {79,104,-37,-66,24,123,30,-26,-99,-8,80,-38,19,14,-127,-3} into Unicode character with ISO-8859-1 standard. Can anyone help me .. how should...
2
by: Paul Johnston | last post by:
Hi I have a string which I convert into a list then read through it printing its glyph and numeric representation #-*- coding: utf-8 -*- thestring = "abcd" thelist = list(thestring) for c...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.