How to get 'od' run?

Michael Torrie torriem at gmail.com
Wed Nov 11 22:21:08 EST 2015


On 11/11/2015 08:04 PM, fl wrote:
> Hi,
> 
> I am learning python. I see a previous post has such code:
> 
> 
> 
> 
> 
>    >>> data = '"binääridataa"\n'.encode('utf-8') 
>    >>> f = open('roska.txt', 'wb') 
>    >>> f.write(data) 
>    17 
>    >>> f.close() 
> 
> The .encode methods produced a bytestring, which Python likes to display 
> as ASCII characters where it can and in hexadecimal where it cannot: 
> 
>    >>> data 
>    b'"bin\xc3\xa4\xc3\xa4ridataa"\n' 
> 
> An "octal dump" in characters (where ASCII, otherwise apparently octal) 
> and the corresponding hexadecimal shows that it is, indeed, these bytes 
> that ended up in the file: 
> 
> $ od -t cx1 roska.txt 
 ^^^
This is most likely a bash prompt. Therefore "od" is a program on your
computer.  Nothing to do with Python at all.

To get Python to display \x## hex codes for non-ascii characters in a
byte stream, you can print out the repr() of the byte string.  For example:

print (repr(my_unicode_string.encode('utf-8')))





More information about the Python-list mailing list