Path: csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail From: Tim Chase Newsgroups: comp.lang.python Subject: Re: Considering migrating to Python from Visual Basic 6 for engineering applications Date: Fri, 19 Feb 2016 07:40:57 -0600 Lines: 55 Message-ID: References: <90cc50d2-1ce5-4588-9bfd-a49d439f00dd@googlegroups.com> <14c75a68-0d2e-45cc-8d73-0d71b6a6aea6@googlegroups.com> <9e57761f-26e1-41c5-8e71-23800de1fdd3@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de J9Hfqy6u7foJxJIk4pKqlA223WuDPyXeV5h5UmugiUtw== 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; 'else:': 0.03; 'elif': 0.04; 'subject:Python': 0.05; 'bits': 0.07; '%r"': 0.09; 'mess': 0.09; 'subject:Visual': 0.09; 'read.': 0.13; 'subject: \n ': 0.15; '"an': 0.16; '\'"\'': 0.16; '-tkc': 0.16; '1):': 0.16; 'determining': 0.16; 'float"': 0.16; 'from:addr:python.list': 0.16; 'from:addr:tim.thechases.com': 0.16; 'from:name:tim chase': 0.16; 'int"': 0.16; 'line.split()': 0.16; 'naive': 0.16; 'quoted': 0.16; 'received:10.21': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'rough': 0.16; 'wrote:': 0.16; 'result,': 0.18; ';-)': 0.18; 'parse': 0.22; 'parsing': 0.22; 'space.': 0.22; 'bit': 0.23; 'import': 0.24; 'header:In-Reply-To:1': 0.24; 'module': 0.25; 'example': 0.26; 'figure': 0.27; 'least': 0.27; 'faster,': 0.29; 'character': 0.29; 'convert': 0.29; 'code': 0.30; "can't": 0.32; 'controls': 0.33; 'gives': 0.35; 'false': 0.35; 'item': 0.35; 'but': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'received:10': 0.37; 'charset:us-ascii': 0.37; '(with': 0.38; 'data': 0.39; 'does': 0.39; 'subject:from': 0.39; 'to:addr:python.org': 0.40; 'some': 0.40; 'easy': 0.60; 'your': 0.60; 'email addr:gmail.com': 0.62; 'are?': 0.84; 'received:10.235': 0.84; 'received:23': 0.84 X-Sender-Id: wwwh|x-authuser|tim@thechases.com X-Sender-Id: wwwh|x-authuser|tim@thechases.com X-MC-Relay: Neutral X-MailChannels-SenderId: wwwh|x-authuser|tim@thechases.com X-MailChannels-Auth-Id: wwwh X-MC-Loop-Signature: 1455889424734:4176434361 X-MC-Ingress-Time: 1455889424733 In-Reply-To: <9e57761f-26e1-41c5-8e71-23800de1fdd3@googlegroups.com> X-Mailer: Claws Mail 3.11.1 (GTK+ 2.24.25; x86_64-pc-linux-gnu) X-AuthUser: tim@thechases.com X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21rc2 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:103194 On 2016-02-19 02:47, wrong.address.1@gmail.com wrote: > 2 12.657823 0.1823467E-04 114 0 > 3 4 5 9 11 > "Lower" > 278.15 > > Is it straightforward to read this, or does one have to read one > character at a time and then figure out what the numbers are? -- It's easy to read. What you do with that mess of data is the complex part. They come in as byte-strings, but you'd have to convert them to the corresponding formats: from shlex import shlex USE_LEX = True # False with open('data.txt') as f: for i, line in enumerate(f, 1): if USE_LEX: bits = shlex(line) else: bits = line.split() for j, bit in enumerate(bits, 1): if bit.isdigit(): result = int(bit) t = "an int" elif '"' in bit: result = bit t = "a string" else: result = float(bit) t = "a float" print("On line %i I think that item %i %r is %s: %r" % ( i, j, bit, t, result, )) The USE_LEX controls whether the example code uses string-splitting on white-space, or uses the built-in "shlex" module to parse for quoted strings that might contain a space. The naive way of string-splitting will be faster, but choke on string-data containing spaces. You'd have to make up your own heuristics for determining what type each data "bit" is, parsing it out (with int(), float() or whatever), but the above gives you some rough ideas with at least one known bug/edge-case. But we can't do *all* the work for you ;-) -tkc