About Match Objects in python - python

I'm fairly new to python and am trying to run the below code:
import re
start([default])
email = 'akshay.bung17#gmail.com'
m = re.search('kit',email)
print m.group()
But i keep getting the following error:
start([default])
NameError: name 'start' is not defined
Can anyone please let me know what exactly is happening over here?

As people have said in the comments, this error message means exactly what it says - you never defined start.
I'd strongly suggest reading this article on how to debug small programs.

Related

Why is try: except: not working for this particular line of code with python Wikipedia API?

i am trying to execute this very short code that took out from my bigger code because i was having problems with it. I was able to reproduce the problem (it gave the exact same error).
BACKGROUND INFO: i am just trying to get the first sentence out of this wikipedia search. but because the word i am searching for (kip, means chicken in Dutch) has a wide variety of meanings or something i get an error. i want to bypass this error using try: except: but it keeps displaying the error message anyway.
here is the code that just doesnt seem to work:
import wikipedia
wikipedia.set_lang('nl')
try:
summry = wikipedia.summary('kip', sentences=1)
print(summry + "\n")
except:
print("error")
i have tried replacing except: with this
except wikipedia.exceptions.DisambiguationError:
but it still doesnt work :( it always displays the error code regradless and prints "error" afterwards
/opt/virtualenvs/python3/lib/python3.8/site-packages/wikipedia/wikipedia.py:389:
GuessedAtParserWarning: No parser was explicitly specified, so I'm using the best available HTML
parser for this system ("html5lib"). This usually isn't a problem, but if you run this code on
another system, or in a different virtual environment, it may use a different parser and behave
differently.
The code that caused this warning is on line 389 of the file
/opt/virtualenvs/python3/lib/python3.8/site-packages/wikipedia/wikipedia.py. To get rid of this
warning, pass the additional argument 'features="html5lib"' to the BeautifulSoup constructor.
lis = BeautifulSoup(html).find_all('li')
error
i am using repl.it to program this
if anyone has any idea about why it keeps displaying the error anyway please please let me know :D
First of all, thank you all for commenting :D it helped a lot
At the end the solution that worked for me was inspired from Askold Ilvento's comment
although
with warnings.catch_warnings(): warnings.simplefilter("ignore")
didn't work when i adapted it a little bit it did the job!
this is the code that solved the problem and allowed me to ignore what was actually a warning (not an exception) and stop displaying it
import warnings
warnings.catch_warnings()
warnings.simplefilter("ignore")
i just added this at the start of the script and it solved the problem :D
once again all the credit for this code goes to Askold Ilvento i just had to add a little bit to it to make it work

[Python]-Can not determine variable type by assigning a value to it using a input() func, why?

I’m assigning a numerical value to a variable called code using the input() function in the terminal. Below are my lines of code:
code = input("Please enter your code:\n")
print(code)
print("\n")
print(type(code))
my question though is that, why after doing so I wouldn’t be able to determine the type of my variable called code, using this line of code print(type(code)). My interpreter in VS Code would give me back this error:
Please enter your code:
2
Unable to find thread for evaluation.
Is there something wrong with my debugger settings or Debug Console? or something that I’m missing? I’m pretty new to all of this, so please explain in simple terms. Thanks a lot.
PS. I've set my console to "internalConsole", maybe that's what is causing this !?

Error in Python when using JSON.Load : name 'isinstance' is not defined

Python noob here. I'm trying to execute a Python script but on json.load it fails with the error message name 'isinstance' is not defined.
Since json is a library that comes with the Python installation (3.4.2) I find this very strange. Has anyone encountered anything similar and/or may have a solution?
The code looks like this
try:
prefs_path = os.path.join(os.path.expanduser("~"), ".foo")
prefs_file = open(prefs_path)
prefs_hash = json.load(prefs_file)
except Exception as e:
raise Exception(str(e))
Thanks guys!
Yea, it actually came up that something was wrong in the file I was trying to read. Once I edited it and fixed the problem the Python script works.
Funny tho how a typo in the json file would cause such an error, complaining about isinstance()!

Python NameError: name 'harris' is not defined

I apologize in advance if there is somewhere the same thread of mine.
I am new in Python programming and trying to compute an example from "Programming Computer Vision with Python" written by Jan Erik Solem.
Here is the code of the example (saved in a file called "harris.py"):
https://github.com/jesolem/PCV/blob/master/pcv_book/harris.py
(Be aware that my code ends at line 70)
After a good explanation of this code, in the book is stated: "Try running the following commands:"
im = array(Image.open(’empire.jpg’).convert(’L’))
harrisim = harris.compute_harris_response(im)
filtered_coords = harris.get_harris_points(harrisim,6)
harris.plot_harris_points(im, filtered_coords)
The problems I've encountered are two:
I am not sure where to run those last lines of code, in harris.py
or in a separate python file.
In whichever file I run it, the following error is shown:
harrisim = harris.compute_harris_response(im)
NameError: name 'harris' is not defined
I don't understand why this error is shown, since 'harris' should call the python script harris.py. Or am I wrong?
It is meant to go into a separate file, but you need to import the harris module first:
import harris
before the module is available to your code.
Harris name does not exists in your script (it was not initialised at any time before using it, so Python does not know what harris is). If import harris does not work, it is because the fact that you do not have any module called that way.

Getting Started with Python: Attribute Error

I am new to python and just downloaded it today. I am using it to work on a web spider, so to test it out and make sure everything was working, I downloaded a sample code. Unfortunately, it does not work and gives me the error:
"AttributeError: 'MyShell' object has no attribute 'loaded' "
I am not sure if the code its self has an error or I failed to do something correctly when installing python. Is there anything you have to do when installing python like adding environmental variables, etc.? And what does that error generally mean?
Here is the sample code I used with imported spider class:
import chilkat
spider = chilkat.CkSpider()
spider.Initialize("www.chilkatsoft.com")
spider.AddUnspidered("http://www.chilkatsoft.com/")
for i in range(0,10):
success = spider.CrawlNext()
if (success == True):
print spider.lastUrl()
else:
if (spider.get_NumUnspidered() == 0):
print "No more URLs to spider"
else:
print spider.lastErrorText()
# Sleep 1 second before spidering the next URL.
spider.SleepMs(1000)
And what does that error generally
mean?
An Attribute in Python is a name belonging to an object - a method or a variable. An AttributeError means that the program tried to use an attribute of an object, but the object did not have the requested attribute.
For instance, string objects have the 'upper' attribute, which is a method that returns the uppercase version of the string. You could write a method that uses it like this:
def get_upper(my_string):
return my_string.upper()
However, note that there's nothing in that method to ensure that you have to give it a string. You could pass in a file object, or a number. Neither of those have the 'upper' attribute, and Python will raise an Attribute Error.
As for why you're seeing it in this instance, you haven't provided enough detail for us to work it out. Add the full error message to your question.
1) Put code in Try ... Except block. get exception details.
2) Could you tell StackTrace details means which line # and method thrown error
And also are you able to run other simple python scripts without any error. Means just try to run some sample script etc.

Categories

Resources