0

SYNTAX ERROR (please copy/paste to see)

x = {
"0"​:​"G"​,
"1":"H"
}

Tested on line interpreter:

>>> x = {
... "0"​:​"G"​
  File "<stdin>", line 2
    "0"​:​"G"​
       ^
SyntaxError: invalid syntax

But this other (seems the same!) is fine, (please copy/paste to see)

x = {
"0":"G",
"1":"H"
}

The real-life dictionary is longer and complex, but it is from a PDF and I copy/paste to editor (or direct to terminal same result). Perhaps (after comment that confirm) it is a ASCII convertion problem and I need to clean... So que question is "how to sanitize copy/paste source-code that seeems perfect?"


Tested with both:

  • python --version = Python 2.7.17
  • python3 --version = Python 3.6.9
Peter Krauss
  • 13,174
  • 24
  • 167
  • 304
  • 1
    You've got non-ascii, non-Unicode (as far as I can quickly tell) characters in your first set. They're non-printing, so they don't show up on screen. They do show up in a decent editor. You can avoid this by not entering the non-parseable characters. – mpez0 Dec 29 '19 at 00:42
  • Hi @mpez0, thanks (!) . Now that you confirm that it is "non-ASCII bug", I edited the question... See final notes: how to sanitize it for Python? – Peter Krauss Dec 29 '19 at 00:48
  • Hum... test copy/paste https://pteo.paranoiaworks.mobi/diacriticsremover/ – Peter Krauss Dec 29 '19 at 00:50
  • if you use, for example `ascii()` built-in method on your string, you will see that your string contains `\u200b` [Zero Width Space](https://www.fileformat.info/info/unicode/char/200b/index.htm) You might probably check https://stackoverflow.com/questions/31522361/python-getting-rid-of-u200b-from-a-string-using-regular-expressions – Andrej Kesely Dec 29 '19 at 01:06
  • What text editor are you using, OP? – mpez0 Dec 30 '19 at 17:40
  • Hi @mpez0, I was using Ubuntu terminal (on software editions I am using "raw" [Atom](http://atom.io)). Max (below) suggested PyCharm. – Peter Krauss Dec 30 '19 at 17:52
  • @PeterKrauss, I hope there's a way to configure Atom to at least show all the characters in your file. Good Luck. – mpez0 Dec 30 '19 at 17:56

2 Answers2

1

I can't comment, so I will write this as an answer.

Depending on the text editor you are using, I suggest looking for a package that can do this. Here's one for Sublime Text.


There are also some online tools to copy/paste your source and check or sanitize it. Examples:

Peter Krauss
  • 13,174
  • 24
  • 167
  • 304
Exr0n
  • 358
  • 1
  • 9
1

I just tried in 3.6 console - it highlights some odd spacing around first colon and clearly says "SyntaxError: invalid character in identifier". See screenshot

As @mpez0 already commented - when you copy from PDF (Word etc), a bunch of extra characters tag along.

Peter Krauss
  • 13,174
  • 24
  • 167
  • 304
MaxTryk
  • 31
  • 4