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. Advertisements

  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. Advertisements

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. David
    Replies:
    2
    Views:
    655
    Thomas G. Marshall
    Aug 3, 2003
  2. Trevor

    sizeof(str) or sizeof(str) - 1 ?

    Trevor, Apr 3, 2004, in forum: C Programming
    Replies:
    9
    Views:
    953
    CBFalconer
    Apr 10, 2004
  3. tcronj

    str() or repr()

    tcronj, Feb 12, 2004, in forum: Python
    Replies:
    0
    Views:
    450
    tcronj
    Feb 12, 2004
  4. Sullivan WxPyQtKinter

    It is fun.the result of str.lower(str())

    Sullivan WxPyQtKinter, Mar 7, 2006, in forum: Python
    Replies:
    5
    Views:
    515
    Tim Roberts
    Mar 9, 2006
  5. Terry Hancock
    Replies:
    3
    Views:
    403
    =?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=
    Sep 11, 2006
  6. Johny

    Repr or Str ?

    Johny, Feb 6, 2007, in forum: Python
    Replies:
    5
    Views:
    899
    Donn Cave
    Feb 6, 2007
  7. adima

    str() and repr() question

    adima, Apr 26, 2007, in forum: Python
    Replies:
    2
    Views:
    288
    Mark T
    Apr 26, 2007
  8. Mark Janssen
    Replies:
    0
    Views:
    269
    Mark Janssen
    Apr 12, 2013
Loading...