Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #11333 > unrolled thread
| Started by | Julio Cesar Rodriguez Cruz <juliocesarrodriguezcruz@gmail.com> |
|---|---|
| First post | 2011-08-13 00:59 -0400 |
| Last post | 2011-08-19 08:07 -0700 |
| Articles | 5 — 5 participants |
Back to article view | Back to comp.lang.python
How to print non-printable chars?? Julio Cesar Rodriguez Cruz <juliocesarrodriguezcruz@gmail.com> - 2011-08-13 00:59 -0400
Re: How to print non-printable chars?? Nobody <nobody@nowhere.com> - 2011-08-13 06:22 +0100
Re: How to print non-printable chars?? Julio Cesar <juliocesarrodriguezcruz@gmail.com> - 2011-08-12 22:55 -0700
Re: How to print non-printable chars?? coldpizza <vriolk@gmail.com> - 2011-08-18 13:44 -0700
Re: How to print non-printable chars?? jmfauth <wxjmfauth@gmail.com> - 2011-08-19 08:07 -0700
| From | Julio Cesar Rodriguez Cruz <juliocesarrodriguezcruz@gmail.com> |
|---|---|
| Date | 2011-08-13 00:59 -0400 |
| Subject | How to print non-printable chars?? |
| Message-ID | <mailman.2249.1313211585.1164.python-list@python.org> |
Hi all,
If I open an .exe file in any text editor I get lot of odd chars,
what I want is to know how to output those chars if I have the hexadecimal
code. I found out how to do the reverse process with the quopri module,
i.e.:
>>> import quopri
>>> quopri.encodestring('ñè')
'=F1=E8=18'
>>> quopri.decodestring('=F1=E8=18')
'\xf1\xe8\x18'
but how to do the reverse? ...gived '\xf1\xe8\x18', print 'ñè'
any tips?
thanks
Julio Cesar
[toc] | [next] | [standalone]
| From | Nobody <nobody@nowhere.com> |
|---|---|
| Date | 2011-08-13 06:22 +0100 |
| Message-ID | <pan.2011.08.13.05.22.21.718000@nowhere.com> |
| In reply to | #11333 |
On Sat, 13 Aug 2011 00:59:42 -0400, Julio Cesar Rodriguez Cruz wrote:
> Hi all,
> If I open an .exe file in any text editor I get lot of odd chars,
> what I want is to know how to output those chars if I have the hexadecimal
> code. I found out how to do the reverse process with the quopri module,
>
> i.e.:
>>>> import quopri
>>>> quopri.encodestring('ñè')
> '=F1=E8=18'
>>>> quopri.decodestring('=F1=E8=18')
> '\xf1\xe8\x18'
>
> but how to do the reverse? ...gived '\xf1\xe8\x18', print 'ñè'
print(quopri.decodestring('=F1=E8=18'))
or:
sys.stdout.write(quopri.decodestring('=F1=E8=18'))
If you type an expression into the interactive Python interpreter, the
result is converted to a string using repr(); for strings, this converts
8-bit characters to their hexadecimal escape sequences, so that the result
only uses ASCII.
OTOH, the print statement converts values to strings using str(); for
strings, this is an identity operation (i.e. it returns the original
string untouched). Similarly, the .write() method of file objects uses str().
[toc] | [prev] | [next] | [standalone]
| From | Julio Cesar <juliocesarrodriguezcruz@gmail.com> |
|---|---|
| Date | 2011-08-12 22:55 -0700 |
| Message-ID | <f90cd8ab-48ac-406c-9225-2f695c3d4a8c@c19g2000yqe.googlegroups.com> |
| In reply to | #11335 |
On Aug 13, 1:22 am, Nobody <nob...@nowhere.com> wrote:
> On Sat, 13 Aug 2011 00:59:42 -0400, Julio Cesar Rodriguez Cruz wrote:
>
> > Hi all,
> > If I open an .exe file in any text editor I get lot of odd chars,
> > what I want is to know how to output those chars if I have the hexadecimal
> > code. I found out how to do the reverse process with the quopri module,
>
> > i.e.:
> >>>> import quopri
> >>>> quopri.encodestring('ñè ')
> > '=F1=E8=18'
> >>>> quopri.decodestring('=F1=E8=18')
> > '\xf1\xe8\x18'
>
> > but how to do the reverse? ...gived '\xf1\xe8\x18', print 'ñè '
>
> print(quopri.decodestring('=F1=E8=18'))
> or:
> sys.stdout.write(quopri.decodestring('=F1=E8=18'))
>
> If you type an expression into the interactive Python interpreter, the
> result is converted to a string using repr(); for strings, this converts
> 8-bit characters to their hexadecimal escape sequences, so that the result
> only uses ASCII.
>
> OTOH, the print statement converts values to strings using str(); for
> strings, this is an identity operation (i.e. it returns the original
> string untouched). Similarly, the .write() method of file objects uses str().
It just works!! thanks a lot and also for the explanations ;)
Cheers
Julio Cesar
[toc] | [prev] | [next] | [standalone]
| From | coldpizza <vriolk@gmail.com> |
|---|---|
| Date | 2011-08-18 13:44 -0700 |
| Message-ID | <6c15a4a4-02a9-4a04-bd3a-0cb88a96470f@v7g2000vbk.googlegroups.com> |
| In reply to | #11333 |
On Aug 13, 7:59 am, Julio Cesar Rodriguez Cruz
<juliocesarrodriguezc...@gmail.com> wrote:
> Hi all,
> If I open an .exe file in any text editor I get lot of odd chars,
> what I want is to know how to output those chars if I have the hexadecimal
> code. I found out how to do the reverse process with the quopri module,
>
> i.e.:>>> import quopri
> >>> quopri.encodestring('ñè ')
> '=F1=E8=18'
> >>> quopri.decodestring('=F1=E8=18')
>
> '\xf1\xe8\x18'
>
> but how to do the reverse? ...gived '\xf1\xe8\x18', print 'ñè '
>
> any tips?
> thanks
> Julio Cesar
In a web/html environment or in broken ascii-only consoles like the
one on windows, I use the following hack:
print your_unicode_string.encode('us-ascii','xmlcharrefreplace')
This will print unicode chars using pure ASCII symbols which will
display correctly in a web browser and are more readable in a console
than unicode escapes.
[toc] | [prev] | [next] | [standalone]
| From | jmfauth <wxjmfauth@gmail.com> |
|---|---|
| Date | 2011-08-19 08:07 -0700 |
| Message-ID | <1d1b4ed7-5a08-48fa-b512-62d42ec678ed@m38g2000vbn.googlegroups.com> |
| In reply to | #11807 |
On 18 août, 22:44, coldpizza <vri...@gmail.com> wrote:
>
>
> ...
>
> In a web/html environment or in broken ascii-only consoles like the
> one on windows ...
C:\Users\Jean-Michel>echo 'Cet œuf de Lætitia coûte un €uro'
'Cet œuf de Lætitia coûte un €uro'
C:\Users\Jean-Michel>c:\Python27\python
Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> print 'Cet œuf de Lætitia coûte un €uro'
Cet œuf de Lætitia coûte un €uro
>>> import sys
>>> u = unicode('Cet œuf de Lætitia coûte un €uro', sys.stdin.encoding)
>>> print u.encode(sys.stdout.encoding)
Cet œuf de Lætitia coûte un €uro
>>>
C:\Users\Jean-Michel>c:\Python32\python
Python 3.2.1 (default, Jul 10 2011, 21:51:15) [MSC v.1500 32 bit
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> print('Cet œuf de Lætitia coûte un €uro')
Cet œuf de Lætitia coûte un €uro
>>>
PS Cet œuf de Lætitia coûte un €uro ->
This Lætitia's egg costs one €uro'
PS2 "ñ" does not require special attention.
PS3 To the original question: This not a *coding* issue, it is a
character *representation* question.
jmf
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web