473,405 Members | 2,171 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,405 software developers and data experts.

print / str / repr

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


Jul 18 '05 #1
1 1887
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
Jul 18 '05 #2

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

Similar topics

5
by: Batista, Facundo | last post by:
One detail I found, don't now if it's ok: Python 2.1.1 (#4, Mar 8 2002, 12:32:24) on sunos5 >>> a = 'ñ' >>> a '\xf1' >>> print a ñ
2
by: Balaji | last post by:
Hello Eveybody, I have written a method which prints the prefix notation of any expression. here is the method... def PrintPrefix(expr): if expr.__class__==E: print expr.operator,...
0
by: David Bolen | last post by:
I ran into this strange behavior when noticing some missing spaces in some debugging output. It seems that somewhere in the print processing, there is special handling for string contents that...
2
by: Jon Perez | last post by:
There are objects whose repr() is not the same as what gets printed out when you apply the print statement to them. Usually these are complex objects like exceptions. Example: >>> import...
23
by: stewart.midwinter | last post by:
No doubt I've overlooked something obvious, but here goes: Let's say I assign a value to a var, e.g.: myPlace = 'right here' myTime = 'right now' Now let's say I want to print out the two...
9
by: François Pinard | last post by:
Hi, people. I hope someone would like to enlighten me. For any application handling Unicode internally, I'm usually careful at properly converting those Unicode strings into 8-bit strings before...
6
by: Kamilche | last post by:
I have a code snippet here that prints a dict in an arbitrary order. (Certain keys first, with rest appearing in sorted order). I didn't want to subclass dict, that's error-prone, and overkill for...
3
by: James J. Besemer | last post by:
I would like to champion a proposed enhancement to Python. I describe the basic idea below, in order to gage community interest. Right now, it's only an idea, and I'm sure there's room for...
4
by: mrstephengross | last post by:
I would like to get the results of a print operation placed in a string. For instance, you can very easily create a list and print it to stdout: x = print x # Will print What if I want the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.