Path: csiph.com!weretis.net!feeder4.news.weretis.net!storethat.news.telefonica.de!feedme.news.telefonica.de!telefonica.de!fu-berlin.de!uni-berlin.de!not-for-mail From: jmp Newsgroups: comp.lang.python Subject: Re: Pythonic love Date: Tue, 08 Mar 2016 14:25:03 +0100 Lines: 42 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de hn8SjqzL0EU8h2Cx0QIqgggztML5277XMDdUuaenZVWw== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'lines.': 0.07; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python': 0.10; 'things.': 0.15; 'lambda': 0.16; 'received:80.91.229.3': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'wrote:': 0.16; 'memory': 0.17; 'skip': 0.18; '(not': 0.20; 'slightly': 0.23; 'header:In-Reply-To:1': 0.24; 'header:User-Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'wonder': 0.27; 'function': 0.28; 'values': 0.28; 'skip:( 20': 0.28; 'perl': 0.29; 'print': 0.30; 'could': 0.35; 'files,': 0.35; 'quite': 0.35; 'but': 0.36; 'should': 0.36; 'possible.': 0.36; 'to:addr:python-list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'thanks': 0.37; 'received:org': 0.37; 'things': 0.38; 'to:addr:python.org': 0.40; 'received:194': 0.61; 'here.': 0.62; 'charset:windows-1252': 0.62; 'more': 0.63 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: paris.sequans.com User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.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:104334 On 03/07/2016 11:51 PM, Fillmore wrote: > > learning Python from Perl here. Want to do things as Pythonicly as > possible. > > I am reading a TSV, but need to skip the first 5 lines. The following > works, but wonder if there's a more pythonc way to do things. Thanks > > ctr = 0 > with open(prfile,mode="rt",encoding='utf-8') as pfile: > for line in pfile: > ctr += 1 > > if ctr < 5: > continue > > allVals = line.strip().split("\t") > print(allVals) what about a generator expression ? The (not so)new hype: with open(prfile,mode="rt",encoding='utf-8') as pfile: for values in (l.strip().split("\t") for (i, l) in enumerate(pfile) if i >=5): print values slightly dense, could be better with a lambda function tovalues = lambda l: l.strip().split("\t") with open(prfile,mode="rt",encoding='utf-8') as pfile: for values in (tovalues(l) for (i, l) in enumerate(pfile) if i >=5): print values This should even work quite efficiently on big files, because I don't thing no more than one line is in memory at a given time. jm