7bit to 8bit

Discussion in 'Python' started by Daniel Roth, Apr 15, 2004.

  1. Daniel Roth

    Daniel Roth Guest

    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
     
    Daniel Roth, Apr 15, 2004
    #1
    1. Advertising

  2. Daniel Roth

    Mark Rowe Guest

    On 15/04/2004, at 15:05, Daniel Roth wrote:

    > 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:

    >>> print "=F6=E4".decode('quopri').decode('latin-1')

    öä

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

    Hope this helps,

    Mark Rowe
    http://bdash.bluewire.net.nz
     
    Mark Rowe, Apr 15, 2004
    #2
    1. Advertising

  3. Daniel Roth

    Daniel Roth Guest

    Mark Rowe wrote:
    >
    > On 15/04/2004, at 15:05, Daniel Roth wrote:
    >
    >> 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:
    >
    > >>> print "=F6=E4".decode('quopri').decode('latin-1')

    > öä
    >


    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.
    >>> print "=F6=E4".decode('quopri').decode('latin-1')

    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

    > Hope this helps,
    >
    > Mark Rowe
    > http://bdash.bluewire.net.nz
    >
    >


    Thanks for the help

    Daniel
     
    Daniel Roth, Apr 15, 2004
    #3
  4. Daniel Roth

    Peter Hansen Guest

    Daniel Roth wrote:

    > 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.
    > >>> print "=F6=E4".decode('quopri').decode('latin-1')

    > 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:

    >>> s = "=F6=E4".decode('quopri').decode('latin-1')
    >>> print s

    (here you'll get the same error as before)
    >>> s

    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
     
    Peter Hansen, Apr 15, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    5
    Views:
    4,866
    Tim McCoy
    Jun 12, 2005
  2. marko

    8bit to 7bit numbers

    marko, Aug 23, 2003, in forum: Perl
    Replies:
    0
    Views:
    1,180
    marko
    Aug 23, 2003
  3. Web Developer

    char 8bit wide or 7bit wide in c++?

    Web Developer, Jul 31, 2003, in forum: C++
    Replies:
    2
    Views:
    614
    John Harrison
    Jul 31, 2003
  4. humble
    Replies:
    0
    Views:
    909
    humble
    Oct 28, 2006
  5. Irene
    Replies:
    0
    Views:
    242
    Irene
    May 1, 2004
Loading...

Share This Page