473,320 Members | 1,863 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,320 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 1739
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. Löwis":
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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

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.