Question: In Python,How to fix erroe message when I got the error message as : Traceback (most recent call last): File C:UserssanjiOneDriveDesktopPythonweek3Testing.py, line 18, in lst.append(pattern.search(line)[0])

In Python,How to fix erroe message when I got the error message as :

Traceback (most recent call last): File "C:\Users\sanji\OneDrive\Desktop\Python\week3\Testing.py", line 18, in lst.append(pattern.search(line)[0]) TypeError: 'NoneType' object is not subscriptable when I run these codes:

#Python 3.8.5 (tags/v3.8.5:580fbb0, Jul 20 2020, 15:57:54) [MSC v.1924 64 bit (AMD64)] on win32 #Type "help", "copyright", "credits" or "license()" for more information. #>>> # importing the module import re # opening and reading the file with open(r'C:\Users\sanji\OneDrive\Desktop\Python\week3\error_log.txt') as fh: fstring = fh.readlines() # decalring the regex pattern for IP addresses pattern = re.compile(r'(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})') # initializing the list object lst=[] # extracting the IP addresses for line in fstring: lst.append(pattern.search(line)[0]) # displaying the extracted IP adresses print(lst

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!