Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #104184

Re: Regex: Perl to Python

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Subject Re: Regex: Perl to Python
Date Mon, 7 Mar 2016 15:45:26 +1100
Lines 27
Message-ID <mailman.6.1457325934.10335.python-list@python.org> (permalink)
References <nbj0k0$12gk$1@gioia.aioe.org>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
X-Trace news.uni-berlin.de HJaftZWahiTfVyMTAoynMwjwSueeFNnat1xoeqKHarag==
Return-Path <rosuav@gmail.com>
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 <nbj0k0$12gk$1@gioia.aioe.org>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.21
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Xref csiph.com comp.lang.python:104184

Show key headers only | View raw


On Mon, Mar 7, 2016 at 3:38 PM, Fillmore <fillmore_remove@hotmail.com> 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 <module>
>     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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Regex: Perl to Python Fillmore <fillmore_remove@hotmail.com> - 2016-03-06 23:38 -0500
  Re: Regex: Perl to Python Chris Angelico <rosuav@gmail.com> - 2016-03-07 15:45 +1100
  Re: Regex: Perl to Python Terry Reedy <tjreedy@udel.edu> - 2016-03-06 23:48 -0500
    Re: Regex: Perl to Python Rustom Mody <rustompmody@gmail.com> - 2016-03-06 21:53 -0800
    Re: Regex: Perl to Python Rustom Mody <rustompmody@gmail.com> - 2016-03-06 21:53 -0800
  Re: Regex: Perl to Python Peter Otten <__peter__@web.de> - 2016-03-07 08:48 +0100
  Re: Regex: Perl to Python Fillmore <fillmore_remove@hotmail.com> - 2016-03-07 07:50 -0500

csiph-web