Python 标识符中的无效字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14844687/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 12:38:00  来源:igfitidea点击:

Invalid character in identifier

pythonpython-3.x

提问by user2052898

I am working on the letter distribution problem from HP code wars 2012. I keep getting an error message that says invalid character in identifier. What does this mean and how can it be fixed. here is the page with the information. hpcodewars.org/past/cw15/problems/2012ProblemsFinalForPrinting.pdf here is the code

我正在处理 HP 代码War 2012 中的字母分配问题。我不断收到一条错误消息,指出标识符中的字符无效。这是什么意思,如何解决。这是包含信息的页面。hpcodewars.org/past/cw15/problems/2012ProblemsFinalForPrinting.pdf 这里是代码

import  string

def  text_analyzer(text):
'''The text to be parsed and
the number of occurrences of the letters given back
be. Punctuation marks, and I ignore the EOF
simple. The function is thus very limited.

'''
    result =  {}

# Processing
    for  a in  string.ascii_lowercase:
    result [a] =  text.lower (). count (a)

    return  result


def  analysis_result (results):

# I look at the data
    keys =  analysis.keys ()
    values \u200b\u200b=  list(analysis.values \u200b\u200b())
    values.sort (reverse = True )

# I turn to the dictionary and
# Must avoid that letters will be overwritten
    w2 =  {}
    list =  []

    for  key in  keys:
        item =  w2.get (results [key], 0 )
        if  item = =  0 :
            w2 [analysis results [key]] =  [key]
        else :
            item.append (key)
            w2 [analysis results [key]] =  item

# We get the keys
    keys =  list (w2.keys ())
    keys.sort (reverse = True )

    for  key in  keys:
        list =  w2 [key]
        liste.sort ()
        for  a in  list:
            print (a.upper (), "*"  *  key)        


text =  """I have a dream that one day this nation will rise up and live out the true
meaning of its creed: "We hold these truths to be self-evident, that all men
are created equal. "I have a dream that my four little children will one day
live in a nation where they will not be Judged by the color of their skin but
by the content of their character.
# # # """

analysis result =  text_analyzer (text)
analysis_results (results)

回答by abarnert

The error SyntaxError: invalid character in identifiermeans 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:

该错误SyntaxError: invalid character in identifier意味着您在变量名、函数等中间有一些不是字母、数字或下划线的字符。实际的错误信息如下所示:

  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:

如果您想知道实际的垃圾字符是什么,我从您的代码中复制了有问题的行并将其粘贴到 Python 解释器中的字符串中:

>>> 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.

所以,那是\u200b,或零宽度空间。这解释了为什么您在页面上看不到它。最常见的是,您获得这些是因为您从 StackOverflow 或 wiki 等网站或 PDF 文件中复制了一些格式化(非纯文本)代码。

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 SyntaxErrorfrom stay spaces (like = =instead of ==) or underscores turned into spaces (like analysis resultsinstead of analysis_results).

当然,你也至少有两个IndentationError不缩进的东西,至少还有一个SyntaxError来自空格(比如= =代替==)或下划线变成空格(比如analysis results代替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.

问题是,你是如何让你的代码进入这种状态的?如果您使用 Microsoft Word 之类的软件作为代码编辑器,那就是您的问题。使用文本编辑器。如果不是......?好吧,无论根本问题是什么导致您最终得到这些垃圾字符、损坏的缩进和多余的空格,请在尝试修复代码之前修复它。

回答by Stephen R

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.

如果您只是运行模块,则在 IDLE 中不会收到好的错误消息。尝试在 IDLE shell 中输入一个导入命令,你会得到一条信息量更大的错误消息。我有同样的错误,这让一切变得不同。

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

(是的,我从电子书中复制了代码,里面充满了看不见的“错误”字符。)

回答by zwitter689

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',\

不确定这是正确的,但是当我从一篇关于使用 pgmpy 的论文中复制一些代码并将其粘贴到 Spyder 下的编辑器中时,我不断收到“标识符中的无效字符”错误,尽管它对我来说并不坏。特定的线路是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

没有充分的理由我更换了'"整个代码和它的工作。不知道为什么,但它确实有效

回答by Ididntdoit

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.

如果您的键盘设置为英语美国(国际)而不是英语美国,则双引号不起作用。这就是单引号在您的情况下起作用的原因。

回答by Nur Amin Sifat

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

仔细看你的报价,这是对还是错!有时双引号不能正常工作,这取决于您的键盘布局。

回答by Yigit Alparslan

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.

有点晚了,但我遇到了同样的错误,我意识到这是因为我从 PDF 复制了一些代码。检查这两者之间的区别: -?第一个来自敲击键盘上的减号,第二个来自乳胶生成的 PDF。