Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Chris Angelico Newsgroups: comp.lang.python Subject: Re: Regex: Perl to Python Date: Mon, 7 Mar 2016 15:45:26 +1100 Lines: 27 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de HJaftZWahiTfVyMTAoynMwjwSueeFNnat1xoeqKHarag== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.009 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'received:209.85.223': 0.03; 'subject:Python': 0.05; 'cc:addr:python-list': 0.09; 'python': 0.10; 'skip:# 20': 0.13; '2016': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'wrote:': 0.16; 'attribute': 0.18; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'object.': 0.22; '(most': 0.24; 'header:In-Reply-To:1': 0.24; 'mon,': 0.24; "doesn't": 0.26; 'message-id:@mail.gmail.com': 0.27; 'perl': 0.29; 'code': 0.30; 'getting': 0.33; 'traceback': 0.33; 'file': 0.34; 'gives': 0.35; 'received:google.com': 0.35; 'something': 0.35; 'but': 0.36; 'instead': 0.36; 'received:209.85': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'received:209': 0.38; 'wrong': 0.38; 'why': 0.39; 'your': 0.60; 'back': 0.62; '30,': 0.63; 'mar': 0.65; 'chrisa': 0.84; 'to:none': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc; bh=mcgJ2MiIzaLFntahE3EkHIkYNCNCudwXjN8eyFAAX1I=; b=tNdl4p6JTbBCvvNP1LqGHLiaufqy8UHaKfmW04g1HgNcqFerQ956XxxyAahSXItR92 XXHsQ+Kn2ADS+LV7oiF/xql14SvHr+bbUB51J4DitxSdZ32c5bbJNdb8WupaF6+rxyFq Sct5U3R7tSH4XVz/3h+nUmPWc4yOFlLiVe1T2b7AJNK+0c6kWoyi/tTKYtoa7Vk0LBGA faQQMToYntRcl08g4EdzwlhuPOfACHHkO76qgc+KPBrFmbhTnPY4nNi/o4nwYHyr6vp9 T7jxXWiqReTYheiSCG54lOpLeroeOGTjUwOH1GUOC+GfpMLJeRhXhB+25u98KzAghJjN ailQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:cc; bh=mcgJ2MiIzaLFntahE3EkHIkYNCNCudwXjN8eyFAAX1I=; b=CyR6UxQO3b7cUuhEUBPozKE0oPOWo7+PUSiLHau4tQmU1nGcznFbiXFriw8d1C78KK 1c4YK19iQfq9L9n2QzI4priKf21fXr5vfs0r1/G70/PUD95tWvpdipJtdpR1j8PsXRSI /TAMaUufYEjhgy+Yun7CiG+nrkExWZsAF29C/bHWhcNlSI5bJXIt5BunEHrQCYS89PGs +V1pJ9lR0aR9baJVQ7dbDMIW7Q8ug20VCnNMkV/l9fF9AfbX71z4zc9fHQXYhfiscKlA 3E/XFinuwmbEjiVLdrWPbQKIHfEjF4Md+WU6737LNeBY+uk5Sv96PcYio2SgvKXuCcsN QyEg== X-Gm-Message-State: AD7BkJIfnVQATRHN6HfZlDhBm8CUagbfI66JAHAz1ROpdBP1/EuDEpaK3dbuu4avTL1LbWlDWUj8bQwXFpkHUw== X-Received: by 10.107.47.163 with SMTP id v35mr17368009iov.19.1457325926258; Sun, 06 Mar 2016 20:45:26 -0800 (PST) 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:104184 On Mon, Mar 7, 2016 at 3:38 PM, Fillmore wrote: > pattern = re.compile(r"(\d+)\t(.+)$") > with open(fields_Indexfile,mode="rt",encoding='utf-8') as headerfile: > for line in headerfile: > #sys.stdout.write(line) > m = pattern.match(line) > print(m.group(0)) > headerfile.close() > > but I must be getting something fundamentally wrong because: > > Traceback (most recent call last): > File "./slicer.py", line 30, in > print(m.group(0)) > AttributeError: 'NoneType' object has no attribute 'group' > > > why is 'm' a None? When the regex doesn't match, Python gives you back None instead of a match object. Your Perl code has an 'if' to guard that; you can do the same thing: if m: print(m.group(0)) ChrisA