Path: csiph.com!eternal-september.org!feeder.eternal-september.org!news.swapon.de!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:34:49 -0700 Lines: 40 Message-ID: References: <24ed2ddb-aaea-455e-bf45-10e1cd8e8376@googlegroups.com> <564405A4.2060300@gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: 8bit X-Trace: news.uni-berlin.de dq5RiKXvCmCjWm6Gaj5R6Amr7nHPCmX1huR65NmtlYTQ== 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; 'recipes': 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; 'dumps': 0.29; 'url:activestate': 0.29; 'code:': 0.29; 'there.': 0.30; 'print': 0.30; 'post': 0.31; 'computer.': 0.32; 'michael': 0.33; 'message- id:@gmail.com': 0.34; 'url:code': 0.34; 'previous': 0.34; 'there': 0.36; 'to:addr:python-list': 0.36; 'subject:?': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'display': 0.37; 'received:org': 0.37; 'doing': 0.38; '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; 'numerous': 0.66; 'therefore': 0.67; 'subject:get': 0.81; 'url:recipes': 0.84 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: <564405A4.2060300@gmail.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:98669 On 11/11/2015 08:21 PM, Michael Torrie wrote: > 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'))) Also there are numerous recipes for doing standard hex dumps out there. For example, http://code.activestate.com/recipes/142812-hex-dumper/