7bit to 8bit

D

Daniel Roth

Hi all!

seems like I've searched the whole net but still not found an answer to
this question:
how do one easy convert a 8bit-text, which by python seems to be in
7bit, to 8bit? I want to convert '=F6' to 'ö', '=E4' to 'ä' and so on.
of course without doing a replace for all those characters.

Daniel
 
M

Mark Rowe

how do one easy convert a 8bit-text, which by python seems to be in
7bit, to 8bit? I want to convert '=F6' to 'ö', '=E4' to 'ä' and so on.
of course without doing a replace for all those characters.

Strings containing sequences like '=F6' appear to have be latin-1
strings that have been encoding using the "quoted printable" encoding.
The following snippet shows one method of decoding the data:
öä

Alternatively, you should look into the quopri module and it's
decodestring function.

Hope this helps,

Mark Rowe
http://bdash.bluewire.net.nz
 
D

Daniel Roth

Mark said:
Strings containing sequences like '=F6' appear to have be latin-1
strings that have been encoding using the "quoted printable" encoding.
The following snippet shows one method of decoding the data:

öä

When I try your 'line' I get:
# python
Python 2.3.3 (#2, Apr 15 2004, 04:41:13)
[GCC 3.3.3 20040110 (prerelease) (Debian)] on linux2
Type "help", "copyright", "credits" or "license" for more information.Traceback (most recent call last):
File "<stdin>", line 1, in ?
UnicodeEncodeError: 'ascii' codec can't encode characters in position
0-1: ordinal not in range(128)
Alternatively, you should look into the quopri module and it's
decodestring function.

Same here

Thanks for the help

Daniel
 
P

Peter Hansen

Daniel said:
When I try your 'line' I get:
# python
Python 2.3.3 (#2, Apr 15 2004, 04:41:13)
[GCC 3.3.3 20040110 (prerelease) (Debian)] on linux2
Type "help", "copyright", "credits" or "license" for more information.Traceback (most recent call last):
File "<stdin>", line 1, in ?
UnicodeEncodeError: 'ascii' codec can't encode characters in position
0-1: ordinal not in range(128)

Note that the error is coming from the attempt to print, not from
the conversion.

Do this instead to prove that what Mark gave you works:
u'\xf6\xe4'

You can see that the string was converted properly (to a unicode
string) but when you try to print it, because your terminal's
encoding is ascii, it cannot be displayed properly.

-Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top