0
for printJobString in logfile:
    userRegex = re.search('(\suser:\s)(.+?)(\sprinter:\s)', printJobString)
    if userRegex:
        userString = userRegex.group(2)
        pagesInt = int(re.search('(\spages:\s)(.+?)(\scode:\s)', printJobString).group(2))

above is my code, when I run this program in the module I end up getting,

Traceback (most recent call last):
  File "C:\Users\brandon\Desktop\project3\project3\pages.py", line 45, in <module>
    log2hist("log")  # version 2.
  File "C:\Users\brandon\Desktop\project3\project3\pages.py", line 29, in log2hist
    pagesInt = int(re.search('(\spages:\s)(.+?)(\scode:\s)', printJobString).group(2))
AttributeError: 'NoneType' object has no attribute 'group'

I know this error means the search is returning None but I'm not sure how to handle this case. Any help would be appreciated, very new to python and still learning the basics.

I am writing a program that should print out the number of pages a user has. 180.186.109.129 code: k n h user: luis printer: core 2 pages: 32 is a target string, my python file is trying to create a data file that has one line for each user and contains the total number of pages printed

4
  • 1
    This is a regex issue, not a Python issue. You should debug your regex at regexr.com or post a question with your regex pattern and an example target string. Commented Apr 25, 2016 at 20:53
  • edited to try to be more clear in my goal @ap Commented Apr 25, 2016 at 21:03
  • @ap I am trying to use regexr.com but I think the format I am using to search through the file for "user:" "printer:" is correct, I'm unsure what I am doing wrong here, to my knowledge it is scanning for those words and ignoring the white spaces. My thought process is clearly wrong as it is not doing what I believe it should be doing Commented Apr 25, 2016 at 21:49
  • 1
    You're not ignoring white spaces, you're matching them. If there's no space in front of 'user:' for instance, it won't match. You should really close this question and make a new one, and tag it with 'regex'. Commented Apr 25, 2016 at 22:28

1 Answer 1

2

The reason it happens is because your regexp does not find anything and returns None

re.search('(\spages:\s)(.+?)(\scode:\s)') returns None

use an if statement to test if it's not None before you try to group

for printJobString in logfile:
    userRegex = re.search('(\suser:\s)(.+?)(\sprinter:\s)', printJobString)
    if userRegex:
        userString = userRegex.group(2)
        pagesInt = re.search('(\spages:\s)(.+?)(\scode:\s)', printJobString)

        if pagesInt:
           pagesInt = int(pageInts.group(2))
Sign up to request clarification or add additional context in comments.

2 Comments

I added that to my code but I get the error Traceback (most recent call last): File "C:\Users\brandon\Desktop\project3\project3\pages.py", line 48, in <module> log2hist("log") # version 2. File "C:\Users\brandon\Desktop\project3\project3\pages.py", line 30, in log2hist if pageInt: UnboundLocalError: local variable 'pageInt' referenced before assignment @danidee
@bkennedy because your if userRegex doesn't eval to true, your pagesInt never gets assigned; then, when you check it in the next if it's not there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.