我在将代码导入python解释器(powershell)时遇到问题。我通过powershell打开python,当我输入“import ex24”时,根本没有出现任何内容,这是使用我从他的网站复制和粘贴的代码(只是为了确定):
def break_words(stuff):
"""This function will break up words for us."""
words = stuff.split(' ')
return words
def sort_words(words):
"""Sorts the words."""
return sorted(words)
def print_first_word(words):
"""Prints the first word after popping it off."""
word = words.pop(0)
print word
def print_last_word(words):
"""Prints the last word after popping it off."""
word = words.pop(-1)
print word
def sort_sentence(sentence):
"""Takes in a full sentence and returns the sorted words."""
words = break_words(sentence)
return sort_words(words)
def print_first_and_last(sentence):
"""Prints the first and last words of the sentence."""
words = break_words(sentence)
print_first_word(words)
print_last_word(words)
def print_first_and_last_sorted(sentence):
"""Sorts the words then prints the first and last one."""
words = sort_sentence(sentence)
print_first_word(words)
print_last_word(words)
当他执行时,他得到了这个:
>>> import ex25
>>> sentence = "All good things come to those who wait."
>>> words = ex25.break_words(sentence)
>>> words
['All', 'good', 'things', 'come', 'to', 'those', 'who', 'wait.']
>>> sorted_words = ex25.sort_words(words)
>>> sorted_words
['All', 'come', 'good', 'things', 'those', 'to', 'wait.', 'who']
>>> ex25.print_first_word(words)
All
>>> ex25.print_last_word(words)
wait.
>>> wrods
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'wrods' is not defined
>>> words
['good', 'things', 'come', 'to', 'those', 'who']
>>> ex25.print_first_word(sorted_words)
All
>>> ex25.print_last_word(sorted_words)
who
>>> sorted_words
['come', 'good', 'things', 'those', 'to', 'wait.']
>>> sorted_words = ex25.sort_sentence(sentence)
>>> sorted_words
['All', 'come', 'good', 'things', 'those', 'to', 'wait.', 'who']
>>> ex25.print_first_and_last(sentence)
All
wait.
>>> ex25.print_first_and_last_sorted(sentence)
All
who
另外,当我像往常一样手动输入代码时,我收到此错误。我无法想象我犯了什么错误:
导入ex25 Traceback(最近一次调用最后一次): 文件“”,第1行,in 文件“ex25.py”,第1行 SyntaxError:第1行文件ex25.py中的非ASCII字符'\ xff',但未声明编码; eps / pep-0263.html了解详情
这是我的手册(ex25):
def break_words(stuff):
"""This function will break up words for us."""
words = stuff.split(' ')
return words
def sort_words(words):
"""Sorts the words"""
return sorted (words)
def print_first_word(words):
"""Prints the first word after popping it off"""
word = words.pop(0)
print word
def print_last_word(words):
"""Prints the last word after popping it off"""
word = words.pop(-1)
print word
def sort_sentence(sentence):
"""Takes in a full sentence and returns the sorted words."""
words = break_words(sentence)
return sort_words(words)
def print_first_and_last(sentence):
"""Prints the first and last words of the sentence."""
words = break_words(sentence)
print_first_word(words)
print_last_word(words)
def print_first_and_last_sorted(sentence):
"""Sorts the words then prints the first and last one."""
words = sort_sentence(sentence)
print_first_word(words)
print_last_word(words)
答案 0 :(得分:1)
好像你的编辑器在文件开头放了一个BOM。这些标记在许多编辑中都是不可见的。
BOM在UTF-8中没有任何意义,所以只需设置您的编辑器以保存“没有BOM的unicode”或等效(应该在settings
或preferences
中的某处)。
字节顺序标记(BOM)是一个Unicode字符,用于表示文本文件或流的字节顺序(字节顺序)。它以U + FEFF字节顺序标记(BOM)编码。 BOM使用是可选的,如果使用,应该出现在文本流的开头。
在UTF-16或UTF-32中,16位或32位单位可能以big-endian或little-endian字节顺序表示,具体取决于平台。
由于UTF-8以字节存储,并且每个平台中的字节都相同,因此发送字节序的信号是无用的。为什么Unicode标准允许使用UTF-8的BOM - 而且最糟糕的是,为什么有些编辑如果不需要也不推荐这样做,这是我无法达到的目标(愚蠢和笨拙)。