I am learning python so this may be a simple question for alot of you and i hope to get some help in understanding what is going wrong.
I am trying to create a function that searches a text for phone numbers.
import re
def findPhoneNumber(a):
b = re.compile(r'\d{3}-\d{3}-\d{4}')
c = b.search(a)
return c.group()
findPhoneNumber('123')
I am getting this error:
AttributeError: 'NoneType' object has no attribute 'group'
so for my understandings, the c variable is not getting any values associated and thus is returning this error.
Can anyone explain what I am doing wrong here?
Your example "123" does not match any phone number, you need "123-456-7890".
So in your example c==None. You have to test that c is an actual match object with if(c) before trying to access c.group()
If your regex doesn't match anything c would be None giving you the next exception:
AttributeError: 'NoneType' object has no attribute 'group'
You just need to handle that exception
def findPhoneNumber(a):
try:
return re.search(r'\d{3}-\d{3}-\d{4}', a).group()
except AttributeError:
return None
Also it has little sense to compile the regex inside the function to use it just one time
Related
The error I'm getting is:
TypeError: 'str' object is not callable
I searched through some other questions, they all said that i redefined a function earlier in my code, but i could not find any occasion where I did this.
My code is:
firstElement = driver.find_elements_by_xpath('//*[#id="assessment-leaderboard"]/li[1]')
print(firstElement)
if len(firstElement) > 0:
print(firstElement[0].text())
else:
print('no data')
Probably, the issue is that .text is not a function but an attribute, so you might want to try
print(firstElement[0].text)
without ().
After one month I decided to work on my app again. I open it and run it and I got this error: AttributeError: 'NoneType' object has no attribute 'find'. I remember that it worked fine, maybe there was a beautifulsoup update or the website (real estate) changed something? But no, the code is the same.
Here's the line that causes trouble according to the compiler:
propertyQuantity = soup.find("h1", {"class":"list-result-title"}).find("b", recursive = False).text
I can post the whole code if necessary. Do you notice something wrong with that line?
soup.find() returns None if it can't find the element you specify. You need to check for this before trying to invoke other methods on the result. So split it up into two statements:
h1 = soup.find("h1", {"class":"list-result-title"})
if h1:
property = find("b", recursive = False)
if property:
propertyQuantity = property.text
else:
// report problem finding `b`
else:
// report problem finding h1
I'm trying to use pyDictionary in a google app engine application but I get an error. I don't get this error when I use it outside the google application. I've added it as a third party library (properly) so it actually imports it without errors so I don't know why.
here's the error:
searching.py", line 63, in getkey assert isinstance(word.keys, object)
AttributeError: 'NoneType' object has no attribute 'keys'
and here's the code for that function:
def getkey(term):
dictionary = PyDictionary()
word = dictionary.meaning(term)
assert isinstance(word.keys, object)
results = word.keys()
newresults = []
for result in results:
newresults.append(str(result))
return newresults[0]
it works outside the app engine project, but not inside...
You should go back and look at your the exception and try and understand what it is telling you.
searching.py", line 63, in getkey assert isinstance(word.keys, object)
AttributeError: 'NoneType' object has no attribute 'keys'
Let's trace it back through your code.
For that error to occur means that value referred to by word is None.
This means that the result of word = dictionary.meaning(term)
Is None.
So you should therefore look at what the value of term is.
Are you validating that term is meaningful anywhere ? If it is, what is the expected result of dictionary.meaning(term) and then investigate why you don't get the expected result.
Some basic debugging would go a long way here, helping you diagnose the issue.
I wrote a piece of code in python that reads a string, split it into 2 parts,the first being a string again and the second being an integer.
For example
ABDKEK 1255443
The code is as follows:
L=raw_input()
ls=L.split()
num=int(ls[1])
L=ls[0]
len=len(L)
and it gives the following error
len=len(L)
TypeError: 'int' object is not callable
I make the following change in the code:
length=len(L)
and it works.
Can anyone explain what does the error 'int' object is not callable mean??
len is a function name which is already defined and should not be use as a variable. Try some other name instead.
I can't say I fully understand the script, because classes go beyond me as yet. Anyway, I've downloaded the py-omegle module from here . And I don't seem to be able to get it to run.
Hoping that I don't need to post the whole class including functions, the part in particular that I'm having trouble with pertains to urllib2 - so I guess It's not too specific an issue - the line that causes all of the issues is:
self.connector = urllib2.build_opener(processor),urllib2.HTTPHandler(debuglevel=1)
and it's not letting me:
#omegle.py
[ln33] self.connector.addheaders = [
[ln34] ('User-agent',user_agent)
[ln35] ]
# or
[ln98] self.id = self.connector.open(self.url+'start',data={}).read().strip('"')
Both return AttributeError:
AttributeError: 'tuple' object has no attribute 'addheaders'
# and further down
AttributeError: 'tuple' object has no attribute 'open'
Could someone please explain how to fix this? I'm sure it has something to do with the first line I posted. The entire source of the ONLY file in this module can be accessed here.
I think it's a case of misplaced parentheses.
The first line:
self.connector = urllib2.build_opener(processor),urllib2.HTTPHandler(debuglevel=1)
creates a tuple consisting of
urllib2.build_opener(processor)
and
urllib2.HTTPHandler(debuglevel=1)
And then assigns this tuple to self.connector.