Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #104185
| Path | csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail |
|---|---|
| From | Terry Reedy <tjreedy@udel.edu> |
| Newsgroups | comp.lang.python |
| Subject | Re: Regex: Perl to Python |
| Date | Sun, 6 Mar 2016 23:48:47 -0500 |
| Lines | 53 |
| Message-ID | <mailman.7.1457326155.10335.python-list@python.org> (permalink) |
| References | <nbj0k0$12gk$1@gioia.aioe.org> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=utf-8; format=flowed |
| Content-Transfer-Encoding | 7bit |
| X-Trace | news.uni-berlin.de HZOYwGPCz2L5CiOiVxbXgQC1Obdl0ymNHzofkRJBTmyg== |
| Return-Path | <python-python-list@m.gmane.org> |
| 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; 'block.': 0.09; 'exiting': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python': 0.10; 'python.': 0.11; 'jan': 0.11; 'skip:# 20': 0.13; 'motivator': 0.16; 'received:80.91.229.3': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'reedy': 0.16; 'statement.': 0.16; 'wrote:': 0.16; 'attribute': 0.18; 'string,': 0.18; '>>>': 0.20; 'trying': 0.22; 'seems': 0.23; 'import': 0.24; '(most': 0.24; 'header:In-Reply-To:1': 0.24; 'module': 0.25; 'header:User-Agent:1': 0.26; 'header:X-Complaints- To:1': 0.26; 'found.': 0.27; 'object,': 0.27; 'function': 0.28; 'perl': 0.29; "i'm": 0.30; 'print': 0.30; 'statement': 0.32; 'getting': 0.33; 'traceback': 0.33; 'file': 0.34; 'add': 0.34; 'returning': 0.35; 'something': 0.35; 'but': 0.36; 'to:addr :python-list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'delete': 0.38; 'wrong': 0.38; 'files': 0.38; 'hi,': 0.38; 'why': 0.39; 'to:addr:python.org': 0.40; 'challenge': 0.61; '30,': 0.63; 'received:96': 0.63; "'if": 0.84; 'hardest': 0.91; 'received:fios.verizon.net': 0.91 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| X-Gmane-NNTP-Posting-Host | pool-96-227-207-81.phlapa.fios.verizon.net |
| User-Agent | Mozilla/5.0 (Windows NT 10.0; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.6.0 |
| 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:104185 |
Show key headers only | View raw
On 3/6/2016 11:38 PM, Fillmore wrote:
>
> Hi, I'm trying to move away from Perl and go to Python.
> Regex seems to bethe hardest challenge so far.
>
> Perl:
>
> while (<HEADERFILE>) {
> if (/(\d+)\t(.+)$/) {
> print $1." - ". $2."\n";
> }
> }
>
> into python
>
> 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()
Delete this line. Files opened in a with statement are automatically
closed when exiting the block. This is a main motivator and use for the
with statement.
> 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?
Python has a wonderful interactive help facility. Learn to use it.
>>> import re
>>> help(re.match)
Help on function match in module re:
match(pattern, string, flags=0)
Try to apply the pattern at the start of the string, returning
a match object, or None if no match was found.
>>>
Add 'if m is not None:' before accessing m.group.
--
Terry Jan Reedy
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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