print / str / repr

Discussion in 'Python' started by Nikolai Kirsebom, May 18, 2004.

  1. How do I (in a program) achieve the same result as using the print
    statement (interactive).

    In the interpreter:

    >>> s = u "some '\xcf' text"
    >>> print s

    Traceback (most ....):
    UnicodeError: ASCII encoding error: ordinal not in range(128)
    >>> s.encode('latin-1')

    "some '\xcf' text"
    >>> print s.encode('latin-1')

    some '¤' text
    >>> `s.encode('latin-1')`

    '"some \'\\xcf\' text"'

    Thanks for any help.
    Nikolai
    Nikolai Kirsebom, May 18, 2004
    #1
    1. Advertising

  2. Nikolai Kirsebom wrote:

    > How do I (in a program) achieve the same result as using the print
    > statement (interactive).
    >
    > In the interpreter:
    >
    >
    >>>>s = u "some '\xcf' text"
    >>>>print s

    >
    > Traceback (most ....):
    > UnicodeError: ASCII encoding error: ordinal not in range(128)
    >
    >>>>s.encode('latin-1')

    >
    > "some '\xcf' text"
    >
    >>>>print s.encode('latin-1')

    >
    > some '¤' text
    >
    >>>>`s.encode('latin-1')`

    >
    > '"some \'\\xcf\' text"'


    You mean you want to print/write/store the exact line '"some \'\\xcf\'
    text"' ?

    s = u"some '\xcf' text"
    s = `s.encode('latin-1')`
    print repr(s.encode('latin-1')) # prints '"some \'\\xcf\' text"'

    Is this what you're looking for?

    -- Vincent Wehren
    vincent wehren, May 18, 2004
    #2
    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.

Share This Page