Does anyone know why I keep getting an error with this section of my code?
if db_orientation2 =="Z":
a="/C=C\"
elif db_orientation2=="E":
a="\C=C\"
This is the error:
File "<ipython-input-7-25cda51c429e>", line 11
a="/C=C\"
^
SyntaxError: EOL while scanning string literal
The elif is highlighted as red as if the operation is not allowed...
String literals cannot end with a backslash. You'll have to double it:
a="/C=C\\"
# ^
The highlighting of your code also clearly shows the problem.
Related
As an exemple if you run this code:
text = "Hi"
if text == "Hello":
print("Hello")
elif text == "Hi":
emoji = '\U000274C'
print(emoji)
else:
print("")
You will get
"IndentationError: unexpected indent"
as a erro message, but if you run just emoji = '\U000274C' you will get the correct erro
"SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes
in position 0-8: truncated \UXXXXXXXX escape"
Any ideas why? is it a bug or a feature and why?
The correct message is really useful as it makes clear that you forgot a zero in thhe unicode, while the indentation erro is totally useless.
I was expecting a useful erro message, it's not clear to me if this behavior is correct or if it is a bug.
It seems you did not write emoji unicode correctly.
It usually have 8 digits. You only wrote 7 digits, so check this part.
I slightly modified your 'elif' part with a popular example unicode. Unicode and CDLR-name both work and return 😀. It works even on Online-Python-Compiler.
Another try with '\U0000274C', which seems most similar to your unicode, returned ❌.
text = "Hi"
if text == "Hello":
print("Hello")
elif text == "Hi":
emoji = "\U0001f600"
# emoji = "\N{grinning face}"
print(emoji)
else:
print("")
Since both the code snippets are starting with a number, it should give same error message. Why are different error messages are given in the two lines?
12_abc=12
^
SyntaxError: invalid decimal literal
12abc=12
^
SyntaxError: invalid syntax
the problem is by your way of declaring the variables . You can not declare a variable starting with a number, it can contain number , so python is not made to understand such variable name . You could name it anything else ...
If the problem was more clarified i could help you with the problem.
Whenever I put triple quotes around a raw string, the following error occurs:
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 28-29: malformed \N character escape
I was wondering why this is the case and if there is any way to avoid it.
I have tried moving the triple quotes to align with various parts of the code but nothing has worked so far.
This runs without error:
final_dir = (r'C:\Documents\Newsletters')
'''
path_list = []
for file in os.listdir(final_dir):
path = os.path.join(final_dir, file)
path_list.append(path)
'''
But then this creates an error:
'''
final_dir = (r'C:\Documents\Newsletters')
path_list = []
for file in os.listdir(final_dir):
path = os.path.join(final_dir, file)
path_list.append(path)
'''
In a string literal like '\N', \N has a special meaning:
\N{name} Character named name in the Unicode database
from String and Bytes literals - Python 3 documentation
For example, '\N{tilde}' becomes '~'.
Since you're quoting code, you probably want to use a raw string literal:
r'\N'
For example:
>>> r"""r'C:\Documents\Newsletters'"""
"r'C:\\Documents\\Newsletters'"
Or you could escape the backslash:
'\\N'
The error doesn't occur for \D because it doesn't have a special meaning.
Thanks to deceze for practically writing this answer in the comments
I've got a Python script which has a class defined with this method:
#staticmethod
def _sanitized_test_name(orig_name):
return re.sub(r'[`‘’\"]*', '', re.sub(r'[\r\n\/\:\?\<\>\|\*\%]*', '', orig_name.encode('utf-8')))
I'm able to run the script from the command prompt just fine, without any issues. But when I paste the code of the full class in the console, I get the SyntaxError: unexpected character after line continuation character:
>>> return re.sub(r'[`‘’\"]*', '', re.sub(r'[\r\n\/\:\?\<\>\|\*\%]*', '', orig_name.encode('utf-8')))
File "<stdin>", line 1
return re.sub(r'[``'\"]*', '', re.sub(r'[\r\n\/\:\?\<\>\|\*\%]*', '', orig_name.encode('utf-8')))
^
SyntaxError: unexpected character after line continuation character
If I skip that method while pasting, it works. Note that there is a difference in what my original line is and what's shown for the error: r'[`‘’\"]*' vs r'[``'"]*'. Replacing that with ur'[`‘’"]*' gives SyntaxError: EOL while scanning string literal.
It seems the Python console is seeing ‘ that as a stylised ` (backtick) and the ’ as a sytlised ' (single quote). When I really mean the unicode open and close quotes. I've got # -*- coding: utf-8 -*- at the top of my script, which I paste into the console as well.
Focusing on just the expression causing the error r'[`‘’"]*'...
>>> r'[`‘’"]*'
File "<stdin>", line 1
r'[``'"]*'
^
SyntaxError: EOL while scanning string literal
>>> ur'[`‘’"]*' # with the unicode modifier
File "<stdin>", line 1
ur'[``'"]*'
^
SyntaxError: EOL while scanning string literal
If the terminal I'm in doesn't accept unicode input, that interpretation of the unicode chars from ‘ to ` and ’ to ', occurs.
So the workaround is to split the regex and use unichr() with the corresponding codes for the two quotes, 2018 and 2019:
>>> r'[`' + unichr(2018) + unichr(2019) + r'"]*'
u'[`\u07e2\u07e3"]*'
(And the raw string modifier r'' probably isn't required for this particular regex.)
I have a question in python's error case. I have tried here....
>>> 0o08
SyntaxError: invalid syntax
>>> 0o8
SyntaxError: invalid token
I want to know:
Which is the invalid token, 0, o or 8?
Why is 0o08 invalid syntax?
An integer literal starting with 0o is interpreted as octal. Per the documentation:
octinteger ::= "0" ("o" | "O") octdigit+ # '0o' or '0O' followed by one or more...
...
octdigit ::= "0"..."7" # ...digits 0 to 7 inclusive
The token 8 is not a valid octdigit, so is not allowed in an octal literal, hence "invalid token".
The reason that the error messages are different is that (related to Python's LL(1) parser, which only looks ahead one token at a time):
If the first token after 0o is not an octdigit, that is clearly an invalid token and parsing stops immediately; whereas
If subsequent tokens happen to be invalid, this isn't detected at such an early stage and parsing continues until the whole line gets rejected as invalid syntax.
You can see this difference in the highlighting in IDLE (only 0o highlighted vs. whole line highlighted), and if you try some alternatives:
>>> 0ok # first token is invalid
SyntaxError: invalid token
>>> 0o18 # subsequent token is invalid
SyntaxError: invalid syntax
>>> 0o10 # all tokens are valid
8