Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Roel Schroeven Newsgroups: comp.lang.python Subject: Re: Considering migrating to Python from Visual Basic 6 for engineering applications Date: Sat, 20 Feb 2016 23:28:40 +0100 Lines: 52 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=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de rU+UNktnSpPpdhX8WgT6KAnTnOS6rs3r0cLDEnmtUs4Q== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'subject:Python': 0.05; 'lines,': 0.05; 'converts': 0.07; 'fixed,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:Visual': 0.09; 'example:': 0.10; 'assume': 0.11; 'def': 0.13; 'subject: \n ': 0.15; 'integers,': 0.16; 'received:80.91.229.3': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'splits': 0.16; 'string': 0.17; 'basically': 0.18; 'thanks.': 0.18; 'aspect': 0.22; '(or': 0.23; 'second': 0.24; 'header:In- Reply-To:1': 0.24; 'header:User-Agent:1': 0.26; 'header:X -Complaints-To:1': 0.26; 'figure': 0.27; 'question': 0.27; 'character': 0.29; 'code': 0.30; 'received:be': 0.30; 'received:84': 0.32; 'point': 0.33; 'third': 0.33; 'smart': 0.33; 'could': 0.35; 'text': 0.35; 'knowledge': 0.35; 'there': 0.36; 'lines': 0.36; 'faster': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'received:org': 0.37; 'desired': 0.37; 'files': 0.38; 'data': 0.39; 'format': 0.39; 'does': 0.39; 'subject:from': 0.39; 'enough': 0.39; 'to:addr:python.org': 0.40; 'email addr:gmail.com': 0.62; 'life': 0.67; 'are?': 0.84; 'fourth': 0.84; 'isaac': 0.84; 'schreef': 0.84 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: d54c57c9e.access.telenet.be User-Agent: Thunderbird 2.0.0.24 (Windows/20100228) In-Reply-To: <9e57761f-26e1-41c5-8e71-23800de1fdd3@googlegroups.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:103279 wrong.address.1@gmail.com schreef op 2016-02-19 11:47: > Thanks. The data I will often have to read from text files could read like > > 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? The question is: what is known about the format of the data? Is it fixed, or does the code need to be smart enough to be able to deal with varying formats? If we can assume that there are 4 lines, of which the first line contains floating point numbers, the second contains integers, the third contains one string and the fourth contains one floating point number, it's pretty easy. For example: def readinput(filename): with open(filename, 'rt') as f: lines = f.readlines() line0 = [float(part) for part in lines[0].split()] line1 = [int(part) for part in lines[1].split()] line2 = lines[2].strip()[1:-1] line3 = float(lines[3]) return line0, line1, line2, line3 line0, line1, line2, line3 = readinput('input.txt') print(line0) print(line1) print(line2) print(line3) Output: [2.0, 12.657823, 1.823467e-05, 114.0, 0.0] [3, 4, 5, 9, 11] Lower 278.15 This basically splits the two first line by whitespace, then converts each part (or the whole line in case of the last two lines) into the desired data type. -- The saddest aspect of life right now is that science gathers knowledge faster than society gathers wisdom. -- Isaac Asimov Roel Schroeven