Path: csiph.com!feeder.erje.net!2.eu.feeder.erje.net!newsfeed0.kamp.net!newsfeed.kamp.net!fu-berlin.de!uni-berlin.de!not-for-mail From: MRAB Newsgroups: comp.lang.python Subject: Re: Pyhon 2.x or 3.x, which is faster? Date: Tue, 8 Mar 2016 00:47:44 +0000 Lines: 44 Message-ID: References: <87d1r6iltx.fsf@elektro.pacujo.net> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de cfRRB7U1n07xz2bUXObQbAKob5s/FOsV/jHEpZWlN+2A== 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; 'binary': 0.05; 'literal': 0.09; 'subject:which': 0.09; 'python': 0.10; 'output': 0.13; "'data'": 0.16; '2016': 0.16; 'binary.': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'received:192.168.1.4': 0.16; 'received:84.93': 0.16; 'received:84.93.230': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 's))': 0.16; 'unicode.': 0.16; "where's": 0.16; 'wrote:': 0.16; 'string': 0.17; 'byte': 0.18; 'string,': 0.18; '>>>': 0.20; 'file:': 0.22; 'am,': 0.23; 'code,': 0.23; 'tried': 0.24; 'header:In-Reply-To:1': 0.24; 'header': 0.24; 'sort': 0.25; 'header:User-Agent:1': 0.26; 'chris': 0.26; 'object,': 0.27; 'sequence': 0.27; 'array': 0.29; 'read,': 0.29; "i'm": 0.30; 'received:84': 0.32; 'tue,': 0.34; 'file': 0.34; 'skip:d 20': 0.34; 'text': 0.35; 'but': 0.36; 'to:addr:python- list': 0.36; 'subject:?': 0.36; 'subject:: ': 0.37; 'skip:s 40': 0.38; 'received:192': 0.39; 'to:addr:python.org': 0.40; 'where': 0.40; 'still': 0.40; "you'll": 0.61; 'mar': 0.65; '(is': 0.84 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.1 cv=bsGxfxui c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=L9H7d07YOLsA:10 a=9cW_t1CCXrUA:10 a=s5jvgZ67dGcA:10 a=IkcTkHD0fZMA:10 a=5ANokUeuAAAA:8 a=gLMALmWlP4CBujsrdgAA:9 a=QEXdDO2ut3YA:10 X-AUTH: mrabarnett@:2500 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.6.0 In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21 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:104287 On 2016-03-08 00:22, BartC wrote: > On 07/03/2016 23:40, Chris Angelico wrote: >> On Tue, Mar 8, 2016 at 9:39 AM, BartC wrote: > >>> I'm using it because this kind of file reading in Python is a mess. If I do >>> a read, will I get a string, a byte sequence object, a byte-array, or >>> array-array, or what? >> >> Uhh.... you'll get either a text string or a byte string, depending on >> whether you opened the file as text or binary. Where's the mess? > > (Is a byte string the same as a byte array? Is a byte array the same as > an array.array? If I remove this line from my code, where 'data' has > just been read from a file: > > data=array.array('B',data) > > then it still works - Python 3. But not on Python 2. If I do .read on a > binary file I get a byte string in Python 3, but a string in Python 2. > That sort of mess. > In Python 2, an unmarked string literal _is_ a bytestring; in Python 3, an unmarked string literal is Unicode. > And how do I write that deceptively simple header on the output file > without array.array because I tried all sorts: > > f = open(file+".ppm", "wb") > s="P6\n%d %d\n255\n" % (hdr.width, hdr.height) > sbytes=array.array('B',list(map(ord,s))) > f.write(sbytes) > You don't need to use array.array: sbytes = bytes(map(ord, s)) In Python 3.5, you can use '%' with a bytestring: s = b"P6\n%d %d\n255\n" % (hdr.width, hdr.height) [snip]