Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Michael Torrie Newsgroups: comp.lang.python Subject: Re: How to get 'od' run? Date: Wed, 11 Nov 2015 20:21:08 -0700 Lines: 36 Message-ID: References: <24ed2ddb-aaea-455e-bf45-10e1cd8e8376@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: 8bit X-Trace: news.uni-berlin.de PTpOJAS9C0E5+CwIBionUg0YZwvX6WRJ/8Jx/d4wTyOA== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'string.': 0.04; 'bash': 0.07; 'f.close()': 0.07; "subject:' ": 0.07; 'subject:How': 0.09; 'indeed,': 0.09; 'non-ascii': 0.09; 'example:': 0.10; 'python': 0.10; 'python.': 0.11; "skip:' 30": 0.15; '^^^': 0.16; 'ascii,': 0.16; 'dump"': 0.16; 'from:addr:torriem': 0.16; 'from:name:michael torrie': 0.16; 'hex': 0.16; 'hexadecimal': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'repr()': 0.16; 'subject:run': 0.16; 'wrote:': 0.16; 'byte': 0.18; 'bytes': 0.18; '>>>': 0.20; 'ascii': 0.22; 'file:': 0.22; '(where': 0.23; 'skip:( 40': 0.23; 'skip:b 30': 0.24; 'header:In-Reply-To:1': 0.24; 'all.': 0.24; 'header:User-Agent:1': 0.26; 'code:': 0.29; 'print': 0.30; 'post': 0.31; 'computer.': 0.32; 'message-id:@gmail.com': 0.34; 'previous': 0.34; 'to:addr:python-list': 0.36; 'subject:?': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'display': 0.37; 'received:org': 0.37; 'hi,': 0.38; 'data': 0.39; 'received:192': 0.39; 'to:addr:python.org': 0.40; 'where': 0.40; 'your': 0.60; 'charset:windows-1252': 0.62; 'therefore': 0.67; 'subject:get': 0.81 X-Virus-Scanned: amavisd-new at torriefamily.org User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.7.0 In-Reply-To: <24ed2ddb-aaea-455e-bf45-10e1cd8e8376@googlegroups.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:98668 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')))