Google Groups Home
Help | Sign in
Problem z kodowaniem przy przekierowaniu STDOUT
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  4 messages - Collapse all
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
danher  
View profile
 More options May 11, 3:54 pm
Newsgroups: pl.comp.lang.python
From: danher <dan...@gazeta.pl>
Date: Sun, 11 May 2008 19:54:01 +0000 (UTC)
Local: Sun, May 11 2008 3:54 pm
Subject: Problem z kodowaniem przy przekierowaniu STDOUT
Witam!

Na początek kod:
#v+
#!/usr/bin/python
#encoding=iso-8859-2

if __name__ == '__main__':
    test = 'ąłóż'
    test_uni = unicode(test,"latin2")
    print test_uni
#v-

Przy uruchomieniu "standardowym" wszystko w porządku:
#v+
~$ ./python.py
ąłóż
#v-

Za to kiedy próbuję wyjście przekierować do pliku lub do innego programu przez
rurkę:
#v+
~$ ./python.py > file
Traceback (most recent call last):
  File "./python.py", line 7, in ?
      print test_uni
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-3:
  ordinal not in range(128)

~$ ./python.py | cat
Traceback (most recent call last):
  File "./python.py", line 7, in ?
      print test_uni
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-3:
  ordinal not in range(128)
#v-

Powłoka to GNU bash, wersja 3.2.17. Pytanie brzmi: dlaczego w momencie, kiedy w
powłoce chcę przekierować wyjście skryptu, to interpreter chce zakodować je w
ASCII? I dlaczego w ogóle obchodzi go to, co się dzieje z tym wyjściem? :)

--
Daniel Hernik


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Rob Wolfe  
View profile
 More options May 11, 4:15 pm
Newsgroups: pl.comp.lang.python
From: Rob Wolfe <r...@smsnet.pl>
Date: Sun, 11 May 2008 22:15:05 +0200
Local: Sun, May 11 2008 4:15 pm
Subject: Re: Problem z kodowaniem przy przekierowaniu STDOUT

Gdy piszesz na standardowe wyjście, to interpreter jest na tyle uprzejmy,
że odgaduje kodowanie na podstawie ustawień terminala.
W przypadku przekierowania nie ma żadnej informacji na temat docelowego
kodowania (nie ma sposobu odgadnięcia jakiego kodowania używa plik,
do którego przekierowujesz) i Python nie próbuje zgadywać.
Przy zapisie do pliku kodowanie należy podawać explicit:
python -c "print u'\u03A9'.encode('utf-8')" > file.txt

Gdy bardzo chcesz używać przekierowań, to możesz się posiłkować
takim trikiem:
sys.stdout = codecs.getwriter("utf-8")(sys.stdout)

Wówczas wyjście będzie zawsze kodowane w 'utf-8'.

RW


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Sulsa  
View profile
 More options May 12, 6:26 am
Newsgroups: pl.comp.lang.python
From: Sulsa <su...@dontmail.me>
Date: Mon, 12 May 2008 12:26:53 +0200
Local: Mon, May 12 2008 6:26 am
Subject: Re: Problem z kodowaniem przy przekierowaniu STDOUT
On Sun, 11 May 2008 22:15:05 +0200

Rob Wolfe <r...@smsnet.pl> wrote:

> Gdy piszesz na standardowe wyjście, to interpreter jest na tyle uprzejmy,
> że odgaduje kodowanie na podstawie ustawień terminala.
> W przypadku przekierowania nie ma żadnej informacji na temat docelowego
> kodowania (nie ma sposobu odgadnięcia jakiego kodowania używa plik,
> do którego przekierowujesz) i Python nie próbuje zgadywać.
> Przy zapisie do pliku kodowanie należy podawać explicit:
> python -c "print u'\u03A9'.encode('utf-8')" > file.txt

czegos nie rozumiem, przeciez on to kodowanie podal jako latin2

test_uni = unicode(test,"latin2")
print test_uni


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Sulsa  
View profile
 More options May 12, 6:30 am
Newsgroups: pl.comp.lang.python
From: Sulsa <su...@dontmail.me>
Date: Mon, 12 May 2008 12:30:50 +0200
Local: Mon, May 12 2008 6:30 am
Subject: Re: Problem z kodowaniem przy przekierowaniu STDOUT
On Mon, 12 May 2008 12:26:53 +0200

Sulsa <su...@dontmail.me> wrote:
> czegos nie rozumiem, przeciez on to kodowanie podal jako latin2

pomieszalem kodowanie wyjscia z kodowaniem stringa.

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2008 Google