use*_*898 27 python python-3.x
我正在处理HP代码大战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)
Run Code Online (Sandbox Code Playgroud)
aba*_*ert 59
错误SyntaxError: invalid character in identifier意味着您在变量名称,函数等中间有一些字符,它不是字母,数字或下划线.实际的错误消息将如下所示:
File "invalchar.py", line 23
values = list(analysis.values ())
^
SyntaxError: invalid character in identifier
Run Code Online (Sandbox Code Playgroud)
这告诉你实际问题是什么,所以你不必猜测"我在哪里有一个无效的字符"?好吧,如果你看一下那行,那里就有一堆非打印垃圾字符.拿出来,你就会超越它.
如果你想知道实际的垃圾字符是什么,我从代码中复制了违规行并将其粘贴到Python解释器中的字符串中:
>>> s=' values ??= list(analysis.values ??())'
>>> s
' values \u200b\u200b= list(analysis.values \u200b\u200b())'
Run Code Online (Sandbox Code Playgroud)
那就是\u200b,或者是ZERO WIDTH SPACE.这就解释了为什么你无法在页面上看到它.最常见的是,你得到这些是因为你已经从StackOverflow或wiki这样的网站上复制了一些格式化的(非纯文本)代码,或者从PDF文件中复制了这些代码.
如果您的编辑器没有为您提供查找和修复这些字符的方法,只需删除并重新键入该行.
当然,你还有至少两个IndentationError来自不缩进的东西,至少还有一个SyntaxError来自停留空间(比如= =代替==)或下划线变成空格(比如analysis results代替analysis_results).
问题是,你是如何让你的代码进入这种状态的?如果你使用像Microsoft Word这样的代码编辑器,那就是你的问题.使用文本编辑器.如果不是......好吧,无论根本问题是什么导致你最终得到这些垃圾字符,破碎的缩进和额外的空格,在你尝试修复代码之前修复它.
与前面的答案类似,问题是 Python 解释器无法识别某些字符(可能是不可见的)。因为这通常是由于复制粘贴代码造成的,所以重新键入该行是一种选择。
\n但如果您不想重新输入该行,您可以将代码粘贴到此工具或类似工具中(Google“在线显示 unicode 字符”),它将显示任何非标准字符。例如,
\ns=\' values \xe2\x80\x8b\xe2\x80\x8b= list(analysis.values \xe2\x80\x8b\xe2\x80\x8b())\'\nRun Code Online (Sandbox Code Playgroud)\n变成
\ns=\' values U+200B U+200B\xe2\x80\x8b\xe2\x80\x8b = list(analysis.values U+200B U+200B \xe2\x80\x8b\xe2\x80\x8b())\'\nRun Code Online (Sandbox Code Playgroud)\n然后,您可以从字符串中删除非标准字符。
\n| 归档时间: |
|
| 查看次数: |
169186 次 |
| 最近记录: |