53

I have some code that includes:

def analysis_result (results):
 keys = analysis.keys ()
 values ​​= list(analysis.values ​​())
 values.sort (reverse = True )
 # More code...

But I get an error from the values ​​= list(analysis.values ​​()) line that says SyntaxError: invalid character in identifier. Why? What is wrong with the code, and how do I fix it?

Note carefully that retyping the code as it appears does not cause a SyntaxError, but copying and pasting it does.


The error here is due to non-printing text that cannot be seen in the code example. Do not close the question as not reproducible, and take special care when editing. At some prior point in the question's history, it was edited to try to "show" the invalid character by using a backslash escape sequence. This does not work; we can only see this by treating the code as a string and asking Python for a string representation. While it would also be incorrect to type code that uses backslash escape sequences, the error message would be different.

Karl Knechtel
61.5k14 gold badges134 silver badges194 bronze badges
asked Feb 13, 2013 at 0:54
5
  • 7
    Please post the whole traceback—it will include the line number, and probably a caret pointing at the invalid character, which will make this trivial to answer. Commented Feb 13, 2013 at 0:57
  • 5
    Also, did you actually write this code, or did you copy and paste out of a PDF or HTML file or something? If the latter, what's the source; maybe we can tell you how to copy it properly. Commented Feb 13, 2013 at 1:10
  • @abarnert thanks for your help,but now it says unexpexted character after line continuation character Commented Feb 13, 2013 at 4:38
  • 5
    As I said before, please give the whole traceback, not just a paraphrase of the error message. Python is telling you which line is wrong and why; if you throw away that information and try to get other people to guess which line you screwed up and how, you're wasting everyone's time. Commented Feb 13, 2013 at 18:38
  • Why did you change this line of code? values \u200b\u200b= list(analysis.values \u200b\u200b()) That doesn't make any sense now. Commented Dec 31, 2020 at 22:23

12 Answers 12

113

The error SyntaxError: invalid character in identifier means you have some character in the middle of a variable name, function, etc. that's not a letter, number, or underscore. The actual error message will look something like this:

 File "invalchar.py", line 23
 values = list(analysis.values ())
 ^
SyntaxError: invalid character in identifier

That tells you what the actual problem is, so you don't have to guess "where do I have an invalid character"? Well, if you look at that line, you've got a bunch of non-printing garbage characters in there. Take them out, and you'll get past this.

If you want to know what the actual garbage characters are, I copied the offending line from your code and pasted it into a string in a Python interpreter:

>>> s=' values ​​= list(analysis.values ​​())'
>>> s
' values \u200b\u200b= list(analysis.values \u200b\u200b())'

So, that's \u200b, or ZERO WIDTH SPACE. That explains why you can't see it on the page. Most commonly, you get these because you've copied some formatted (not plain-text) code off a site like StackOverflow or a wiki, or out of a PDF file.

If your editor doesn't give you a way to find and fix those characters, just delete and retype the line.

Of course you've also got at least two IndentationErrors from not indenting things, at least one more SyntaxError from stay spaces (like = = instead of ==) or underscores turned into spaces (like analysis results instead of analysis_results).

The question is, how did you get your code into this state? If you're using something like Microsoft Word as a code editor, that's your problem. Use a text editor. If not... well, whatever the root problem is that caused you to end up with these garbage characters, broken indentation, and extra spaces, fix that, before you try to fix your code.

answered Feb 13, 2013 at 1:08
Sign up to request clarification or add additional context in comments.

4 Comments

thanks for your help,but now it says unexpexted character after line continuation character.
As I already mentioned in the answer, I can immediately spot at least 3 more errors in your code, and from a quick glance I can see even more (like calling a function named analysis_results in the last line when you defined a function named analysis_result). But nobody is going to sit down and try to guess what your code is trying to do and debug all of your problems for you, especially if you provide worse error messages than the interpreter.
This error can occur when copy-pasting math formulas with subtraction signs in them. The sign may be copied as the unicode subtraction sign, "−", instead of the hyphen "-", which the system expects.
I had the same error. After pasting in VSCode, I understood that it was the zero width space character. One can try doing it on Notepad++ as well.
12

If your keyboard is set to English US (International) rather than English US the double quotation marks don't work. This is why the single quotation marks worked in your case.

answered Feb 9, 2018 at 7:33

Comments

6

Similar to the previous answers, the problem is some character (possibly invisible) that the Python interpreter doesn't recognize. Because this is often due to copy-pasting code, re-typing the line is one option.

But if you don't want to re-type the line, you can paste your code into this tool or something similar (Google "show unicode characters online"), and it will reveal any non-standard characters. For example,

s=' values ​​= list(analysis.values ​​())'

becomes

s=' values U+200B U+200B​​ = list(analysis.values U+200B U+200B ​​())'

You can then delete the non-standard characters from the string.

answered Feb 10, 2021 at 15:59

Comments

3

Carefully see your quotation, is this correct or incorrect! Sometime double quotation doesn’t work properly, it's depend on your keyboard layout.

answered Sep 12, 2019 at 2:33

Comments

3

I got a similar issue. My solution was to change minus character from:

to

-
answered Sep 18, 2020 at 18:54

2 Comments

Welcome to Stack Overflow. Before answering an old question having (many) other answers already ensure your answer adds something new or is otherwise helpful in relation to them. As such the answer from Yigit Alparslan points out a similar problem/solution for the minus - sign; however providing more context. – For this question an arbitrary number of answers could be posted by looking at each character individually having a subtle encoding issue. But what's the value of that?
As you're starting out here, please take the tour to learn how Stack Overflow works and also have a look at How do I write a good answer?.
2

I got that error, when sometimes I type in Chinese language. When it comes to punctuation marks, you do not notice that you are actually typing the Chinese version, instead of the English version.

The interpreter will give you an error message, but for human eyes, it is hard to notice the difference.

For example, "," in Chinese; and "," in English. So be careful with your language setting.

answered Dec 9, 2020 at 0:59

Comments

1

Not sure this is right on but when i copied some code form a paper on using pgmpy and pasted it into the editor under Spyder, i kept getting the "invalid character in identifier" error though it didn't look bad to me. The particular line was grade_cpd = TabularCPD(variable='G',\

For no good reason I replaced the ' with " throughout the code and it worked. Not sure why but it did work

Bartłomiej Semańczyk
62k52 gold badges257 silver badges408 bronze badges
answered Nov 20, 2015 at 0:24

1 Comment

I also got this error as a result of copy/pasting code from a gmail draft.
1

A little bit late but I got the same error and I realized that it was because I copied some code from a PDF. Check the difference between these two: - The first one is from hitting the minus sign on keyboard and the second is from a latex generated PDF.

answered Jan 8, 2020 at 21:21

Comments

1

This error occurs mainly when copy-pasting the code. Try editing/replacing minus(-), bracket({) symbols.

answered Oct 8, 2020 at 9:56

Comments

0

You don't get a good error message in IDLE if you just Run the module. Try typing an import command from within IDLE shell, and you'll get a much more informative error message. I had the same error and that made all the difference.

(And yes, I'd copied the code from an ebook and it was full of invisible "wrong" characters.)

answered Jan 1, 2015 at 5:11

Comments

0

My solution was to switch my Mac keyboard from Unicode to U.S. English.

answered Nov 12, 2020 at 13:53

Comments

0

it is similar for me as well after copying the code from my email.

def update(self, k=1, step = 2):
 if self.start.get() and not self.is_paused.get(): U+A0
 
 x_data.append([i for i in range(0,k,1)][-1])
 y = [i for i in range(0,k,step)][-1]

There is additional U+A0 character after checking with the tool as recommended by @Jacob Stern.

answered Nov 23, 2022 at 22:03

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.