472,958 Members | 2,051 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 software developers and data experts.

pipeline encoding

My locale is set to UTF-8. The command:
python -c "print u'\u03A9'"
gives me the desired result and doesn't produce any error.

But when I want to redirect the output to a file I invoke:
python -c "print u'\u03A9'" file.txt
I get an error:

File "<string>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode character u'\u03a9' in
position 0: ordinal not in range(128)

How to cope with it?

-tt.
Dec 6 '07 #1
9 1731
Tomasz Toczyski schrieb:
My locale is set to UTF-8. The command:
python -c "print u'\u03A9'"
gives me the desired result and doesn't produce any error.

But when I want to redirect the output to a file I invoke:
python -c "print u'\u03A9'" file.txt
I get an error:

File "<string>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode character u'\u03a9' in
position 0: ordinal not in range(128)

How to cope with it?
Python tries and guesses the stdout-encoding based on the terminal
settings. So the first print works.

However, piping to a file means that it can't do so, because it doesn't
(and shouldn't) make any assumptions on the output encoding desired -
after all, it might be appending to a XML-file with e.g. latin1 encoding.

So you need to explictely encode the unicode-object with the desired
encoding:
python -c "print u'\u03A9'.encode('utf-8')" file.txt
Diez
Dec 6 '07 #2
Tomasz Toczyski <tt@praterm.com.plwrites:
My locale is set to UTF-8. The command:
python -c "print u'\u03A9'"
gives me the desired result and doesn't produce any error.

But when I want to redirect the output to a file I invoke:
python -c "print u'\u03A9'" file.txt
I get an error:

File "<string>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode character u'\u03a9' in
position 0: ordinal not in range(128)

How to cope with it?
If you print to a terminal Python can use terminal encoding,
but if you redirect to a file Python doesn't know
what encoding to use (e.g. how was encoded existing file)
and refuses to guess.
You have to specify that encoding explicit:
python -c "print u'\u03A9'.encode('utf-8')" file.txt

HTH,
Rob
Dec 6 '07 #3
Diez B. Roggisch:
>
Python tries and guesses the stdout-encoding based on the terminal
settings. So the first print works.

However, piping to a file means that it can't do so, because it doesn't
(and shouldn't) make any assumptions on the output encoding desired -
after all, it might be appending to a XML-file with e.g. latin1
encoding.

So you need to explictely encode the unicode-object with the desired
encoding:
python -c "print u'\u03A9'.encode('utf-8')" file.txt
Thanks. It is a solutiona to my problem but:

Are there any command line option for telling python what encoding to use
for stdout?

To be honest I have a more complicated program than the example that I
have presented - there are many print commands inside and it is not very
feasible for me to put .encode('utf-8') inside every print occurence.

-tt.
Dec 6 '07 #4
Tomek Toczyski schrieb:
Diez B. Roggisch:
>>
Python tries and guesses the stdout-encoding based on the terminal
settings. So the first print works.

However, piping to a file means that it can't do so, because it
doesn't (and shouldn't) make any assumptions on the output encoding
desired - after all, it might be appending to a XML-file with e.g.
latin1 encoding.

So you need to explictely encode the unicode-object with the desired
encoding:
python -c "print u'\u03A9'.encode('utf-8')" file.txt

Thanks. It is a solutiona to my problem but:

Are there any command line option for telling python what encoding to
use for stdout?

To be honest I have a more complicated program than the example that I
have presented - there are many print commands inside and it is not very
feasible for me to put .encode('utf-8') inside every print occurence.
No it hasn't, and it's easy enough remedied by doing
def eprint(msg):
print msg.encode('utf-8')

and then doing

eprint('whatever')

instead of

print 'whatever'

Diez
Dec 6 '07 #5
Are there any command line option for telling python what encoding to
use for stdout?
Not a command line option. However, you can wrap sys.stdout with a
stream that automatically performs an encoding. If all your print
statements output Unicode strings, you can do

sys.stdout = codecs.getwriter("utf-8")(sys.stdout)

HTH,
Martin
Dec 6 '07 #6
Tomek Toczyski <gu*@kajak.org.plwrites:
Are there any command line option for telling python what encoding to
use for stdout?

To be honest I have a more complicated program than the example that I
have presented - there are many print commands inside and it is not
very feasible for me to put .encode('utf-8') inside every print
occurence.
You can use sitecustomize.py [1]_ for that purpose, e.g.
create this file in your current directory:

# sitecustomize.py
import sys
sys.setdefaultencoding('utf-8')

and run Python like that:

PYTHONPATH=. python -c "print u'\u03A9'" file.txt

But remember that when you copy this file to the global
PYTHONPATH on your system it will affect all Python
programs.

... [1] http://docs.python.org/lib/module-site.html

HTH,
Rob
Dec 6 '07 #7
Tomek Toczyski wrote:
Diez B. Roggisch:
>>
Python tries and guesses the stdout-encoding based on the terminal
settings. So the first print works.

However, piping to a file means that it can't do so, because it doesn't
(and shouldn't) make any assumptions on the output encoding desired -
after all, it might be appending to a XML-file with e.g. latin1
encoding.

So you need to explictely encode the unicode-object with the desired
encoding:
python -c "print u'\u03A9'.encode('utf-8')" file.txt

Thanks. It is a solutiona to my problem but:

Are there any command line option for telling python what encoding to use
for stdout?

To be honest I have a more complicated program than the example that I
have presented - there are many print commands inside and it is not very
feasible for me to put .encode('utf-8') inside every print occurence.
Alternatively you can wrap stdout:

# -*- coding: utf-8 -*-
import sys

if sys.stdout.encoding is None:
import locale
import codecs

encoding = locale.getpreferredencoding() # or just "utf-8"
streamwriter = codecs.lookup(encoding).streamwriter
sys.stdout = streamwriter(sys.stdout)

print u"ähnlich üblich möglich"
Dec 6 '07 #8
En Thu, 06 Dec 2007 14:13:04 -0300, Tomasz Toczyski <tt@praterm.com.pl>
escribi:
My locale is set to UTF-8. The command:
python -c "print u'\u03A9'"
gives me the desired result and doesn't produce any error.
Because in this case stdout is bound to your terminal and Python can ask
the OS which encoding it uses.
But when I want to redirect the output to a file I invoke:
python -c "print u'\u03A9'" file.txt
I get an error:

File "<string>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode character u'\u03a9' in
position 0: ordinal not in range(128)
In this case, stdout is redirected, and a file can be written in any
encoding you like. So unless you tell Python which encoding to use, it
refuses to guess. Try:

python -c "print u'\u03A9'.encode('utf-8')" file.txt

Also try: python -c "import sys; print sys.stdout.encoding"
and see what happens in both cases.

--
Gabriel Genellina

Dec 7 '07 #9
"Martin v. Lwis":
Not a command line option. However, you can wrap sys.stdout with a
stream that automatically performs an encoding. If all your print
statements output Unicode strings, you can do

sys.stdout = codecs.getwriter("utf-8")(sys.stdout)
It is the best solution for me.
Thanks.

-tt.
Dec 7 '07 #10

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

Similar topics

0
by: Henning Truslew Gulliksen | last post by:
I need to generat documentation for XML Schmeas in Forrest/DocBook format, and I prefere a layout as close to the html output from XMLSpy as possible. Does someone know about a way of creating...
0
by: Stuart | last post by:
Hi, I am relatively new to Cocoon and have some quick questions as to how it processes its pipelines: 1. I assume that each stage in a pipeline must complete fully before the next phase is...
2
by: Aaron | last post by:
I am kind of new the BizTalk 2004, can someone tell me how I can test my custom (decode) pipeline component in BTS 2004?
0
by: Geoff | last post by:
Does anyone have a "fleshed out" example of Pipeline Component in C#. The example in MSDN doesn't really show much. I am interested in the parameters coming in and the return values. They are...
2
by: Microsoft | last post by:
Hi All. We currently use the Microsoft Commerce Server pipeline component to run the workflow for our application, however this is very limited when it comes to ASP.NET and has no programmable...
0
by: Yanir | last post by:
Hi I want to understand the meaning of assinging the "pipeline" value to the scope attribute of the <object> directive at the global.asax The msdn states that it means the object scope available...
3
by: David Prentice | last post by:
I am having dificulty debugging an ASP.NET Ajax (Novemver CTP) app on windows vista RTM. I have found the following interesting facts but cannot join the dots . . If my application is...
0
by: stylusstudio | last post by:
Dear microsoft.public.dotnet.xml, Stylus Studio has published a new educational video tutorial entitled: Visual Data Integration Using Stylus Studio XML Pipeline. To view this new tutorial,...
0
by: Bharanee K | last post by:
Hi, In my application i'm using WSE policy.When i deploy my web service in another machine it shows the following error message The service pipeline could not be created. ANd it shows that...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

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.