标识符中的无效字符 [英] Invalid character in identifier

查看:251
本文介绍了标识符中的无效字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在解决 HP 代码战争 2012 中的字母分布问题.我不断收到一条错误消息,指出标识符中的字符无效".这是什么意思,如何修复?

这里是包含信息的页面.

导入字符串定义文本分析器(文本):'''要解析的文本和返回的字母出现的次数是.标点符号,我忽略了 EOF简单的.因此功能非常有限.'''结果 = {}# 加工对于 string.ascii_lowercase 中的 a:结果[a] = text.lower().计数(一)返回结果def analysis_result(结果):#我看数据键 = analysis.keys()values \u200b\u200b= list(analysis.values \u200b\u200b())values.sort(反向 = True )# 我翻到字典# 必须避免字母被覆盖w2 = {}列表 = []对于键中的键:item = w2.get (results [key], 0 )如果项目 == 0 :w2 [分析结果[key]] = [key]别的 :item.append(键)w2 [分析结果 [key]] = 项目# 我们拿到钥匙键=列表(w2.keys())keys.sort(反向 = True )对于键中的键:列表 = w2 [键]liste.sort()对于一个列表:打印(a.upper(),*"*键)text = """我有一个梦想,有一天这个国家会崛起并活出真正的其信条的含义:我们认为这些真理是不言而喻的,即所有人生而平等."我有一个梦想,我的四个小孩有一天会生活在一个不会以肤色来评判他们的国家,而是根据他们性格的内容.###""分析结果 = text_analyzer(文本)analysis_results(结果)

解决方案

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

 文件invalchar.py",第 23 行值=列表(分析.值())^语法错误:标识符中的字符无效

这会告诉您实际问题是什么,因此您不必猜测我在哪里有无效字符"?好吧,如果您查看该行,就会发现其中有一堆非打印垃圾字符.把它们拿出来,你就会过去的.

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

<预><代码>>>>s=' values = list(analysis.values ())'>>>秒'值\u200b\u200b=列表(analysis.values\u200b\u200b())'

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

如果您的编辑器没有为您提供查找和修复这些字符的方法,只需删除并重新键入该行.

当然,你也有至少两个 IndentationError 没有缩进,至少还有一个 SyntaxError 来自留位空间(比如 = = 而不是 ==) 或下划线变成空格(比如 analysis results 而不是 analysis_results).

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

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.

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)

解决方案

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.

这篇关于标识符中的无效字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆