Python Forum
AttributeError: 'NoneType' re.search
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
AttributeError: 'NoneType' re.search
#1
python 3.12.1 Windows 11 command prompt (cmd)
TURN THIS:
this85_e1.txt
the other38_4e.asc
INTO THIS:
this
the other

This was working a few days ago and I thought I was running the
same code today. But now I get this error

File "C:\Users\phill\Documents\python\re_search_pub.py", line 9, in <module>
ln = line[0:pos.start()]
^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'start'

I did check out https://docs.python.org/dev/library/re.html
But can't figure out why [pos] is (now) a 'NoneType'

MY CODE:

import re   

ln = ""
pos = 0
output_file = open("bare.txt", "w") 
lines = open("filelist.asc", "r").readlines()
for line in lines:
pos = re.search(r"\d\d_\w\w", line)
    ln = line[0:pos.start()]
    output_file.write(ln + '\n')
output_file.close()
deanhystad write Jan-20-2024, 03:26 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
You read the documentation?
Quote:re.search(pattern, string, flags=0)
Scan through string looking for the first location where the regular expression pattern produces a match, and return a corresponding Match. Return None if no position in the string matches the pattern; note that this is different from finding a zero-length match at some point in the string.
search returns None if there is no pattern match. You need to check that search() returned a match before using the match.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python: Regex is not good for re.search (AttributeError: 'NoneType' object has no att Melcu54 9 1,537 Jun-28-2023, 11:13 AM
Last Post: Melcu54
  AttributeError: 'NoneType' object has no attribute 'group' MaartenRo 3 4,727 Jan-02-2022, 09:53 AM
Last Post: snippsat
  AttributeError: 'NoneType' object has no attribute 'group' MaartenRo 3 2,699 Jan-01-2022, 04:16 PM
Last Post: MaartenRo
  AttributeError: 'NoneType' object has no attribute 'next' loves 2 11,646 Dec-15-2020, 11:30 PM
Last Post: bowlofred
  AttributeError: 'NoneType' object has no attribute 'label' : What's wrong ? Etienne17 1 4,040 Dec-13-2019, 06:57 PM
Last Post: ibreeden
  AttributeError: 'NoneType' object has no attribute 'get' ahmeddedo 1 6,681 May-27-2019, 05:57 AM
Last Post: heiner55
  AttributeError: 'NoneType' object has no attribute 'n' in list of class objects jdrp 4 5,768 Jun-19-2018, 02:44 PM
Last Post: jdrp
  String substitution with dictionary - AttributeError: 'NoneType' Atalanttore 2 3,267 May-02-2018, 08:52 PM
Last Post: Atalanttore
  AttributeError: 'NoneType' object has no attribute 'get' BoaCoder3 2 30,264 Feb-03-2018, 07:17 PM
Last Post: BoaCoder3

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020