This is my first time using Python and I am receiving the attached message when attempting to run a script.
here is the code (line 32 is the line starting "font-size:...":
# County style
'font-size:12px;fill-rule:nonzero;stroke:#FFFFFF;stroke-opacity:1;
stroke-width:0.1;stroke-miterlimit:4;stroke-dasharray:none;stroke-linecap:butt;
marker-start:none;stroke-linejoin:bevel;fill:'
Could any helpful person tell me whats up here?
You are using a multiline string, you need triple quotes (''' or """). Example:
'''font-size:12px;fill-rule:nonzero;stroke:#FFFFFF;stroke-opacity:1;
stroke-width:0.1;stroke-miterlimit:4;stroke-dasharray:none;stroke-linecap:butt;
marker-start:none;stroke-linejoin:bevel;fill:'''
Related
I am a beginner of web scripting.
I was following a tutorial on Edureka: A Beginner’s Guide to learn web scraping with python!.
There is a syntax error shown inside the URL of my script:
driver.get('<a href='https://www.jbhifi.com.au/products/lenovo-ideapad-slim-5i-15-6-full-hd-laptop-512gb-intel-i5'>https://www.jbhifi.com.au/collections/computers-tablets/windows-laptops?page=4')
The invalid syntax seems under com, which is very confusing to me.
I have no idea how to solve it.
The syntax error is because you are enclosing your string with single quotes, and you also have single quotes inside the string. So Python thinks that everything after '<a href=' is not a string, but it can't interpret that other stuff as Python code, so Python gives up and raises an error.
Normally you would deal with this by enclosing the string with double quotes, or by escaping the single quotes. However, with driver.get, you don't use the <a href="..."> part; you just give it the URL. So you can do this:
driver.get('https://www.jbhifi.com.au/collections/computers-tablets/windows-laptops?page=4')
You cannot use same type of quotes inside and out within the same string. You can either use single and double quotes together or you can escape it. Modify your script as follows:
driver.get('<a href="https://www.jbhifi.com.au/products/lenovo-ideapad-slim-5i-15-6-full-hd-laptop-512gb-intel-i5">)
Im going through the book Dive into Python 3 and in one of its parts theres a .py file which contains a function that has a triple quoted string that as the book explains it, is a document.
When I go into the python shell and import said file and write
print(humansize.approximate_size.__doc__)
it gives me back the said triple quoted string.
I decided Id give it a try myself and created a triple quoted string right under the other one. Saved the file and ran the same code in the python shell - but only the first one appeared. Why is that?
Do i need to install some separate tool to document code ?
Thank you!
I'm trying to create a simple string like:
test = "abc#email.com" in Pydev but it automatically interpret "#" as a special symbol and the statement cannot pe done.
When I focus on the variable in Pydev, I can see:
test = "abc*#email.com"* instead of test = "abc#email.com"
Anyone has any idea why I have this issue?
If I run the statement in windows command prompt python, then it is correctly assigned.
Does it have any relation with pylint?
Thanks,
The only way I was able to reproduce similar result was with unclosed string before that, which is obviously a syntax error. Otherwise I've never seen any problems with string literals containing # while using PyDev (with PyLint).
try %#?
this might be your answer. Not a whole lot out there about special characters in python.
I am writing a python script using version 2.7.3. In the script a line is
toolsDir = 'tools/'
When I run this in terminal I get SyntaxError: invalid syntax on the last character in the string 'r'. I've tried renaming the string, using " as opposed to '. If I actually go into python via bash and declare the string in one line and print it I get no error.
I checked the encoding via file -i update.py and I get text/x-python; charset=us-ascii
I have used TextWrangler, nano and LeafPad as the text editors.
I have a feeling it may be something with the encoding of one of the editors. I have had this script run before without any errors.
Any advice would be greatly appreciated.
The string is 'tools/'. toolsDir is a variable. You're free to use different terminology, of course, but you'll end up confusing people trying to help you. The only r in that line is the last character of the variable name, so I assume that's the location of the error.
Most likely you've managed to introduce a fixed-width space (character code 0xA0) instead of an ordinary space. Try deleting SP=SP (all three characters) and retyping them.
Try running the code through pylint.
You probably have a syntax error on a nearby line before this one. Try commenting this line out and see if the error moves.
You might have a whitespace error, don't forget whitespace counts in python. If you've mixed tabs and spaces anywhere in your file it can throw the syntax checker off by several lines.
If you copied and pasted lines into this from any other source you may have copied whitespace in that doesn't fit with whichever convention you used.
The error was, of course, a silly one.
In one of my imports I use try: without closing or catching the error condition. pylint did not catch this and the error message did not indicate this.
If someone in the future has this triple check all opening code for syntax errors.
I'm trying to open a program while I'm in a python script using the subprocess.call() function, It opens the program but for some reason the program doesn't allows that and just throw an "Unhandaled exception" error, I know the problem is probably in the program so there may be any other command that will open a program, fill some fields and press "Submit?"
Thanks!
Edit: I've no code to post..
str = 'd:\Softwares\X.exe'
subprocess.call(str)
I've also tried with:
subprocess.call(str,shell=True)
Try calling another program the same way. If the problem persists, the problem is with your code. If it goes away, the problem is with the program.
I think changing to 'D:/Softwares/X.exe' or one of the other string formats will help because the '\' character is the escape character ... used for example to denote a new line '\n'.
It probably works if you use forward-slashes (backslashes are escape symbols in Python). If it doesn't, write the first line like this:
str = r'd:\Softwares\X.exe'
The r tells Python that you are creating a raw string, so it will ignore escape symbols. More information at: https://docs.python.org/2/reference/lexical_analysis.html#string-literals