Python 语法错误:以“\x91”开头的非 UTF-8 代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23092176/
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
SyntaxError: Non-UTF-8 code starting with '\x91'
提问by GrapeSoda3
I am trying to write a binary search program for a class, and I am pretty sure that my logic is right, but I keep getting a non-UTF-8 error. I have never seen this error and any help/clarification would be great! Thanks a bunch.
我正在尝试为一个类编写一个二进制搜索程序,我很确定我的逻辑是正确的,但我不断收到非 UTF-8 错误。我从未见过这个错误,任何帮助/澄清都会很棒!谢谢一堆。
Here's the code.
这是代码。
def main():
str names = [‘Ava Fischer', ‘Bob White', ‘Chris Rich', ‘Danielle Porter', ‘Gordon Pike', ‘Hannah Beauregard', ‘Matt Hoyle', ‘Ross Harrison', ‘Sasha Ricci', ‘Xavier Adams']
binarySearch(names, input(str("Please Enter a Name.")))
print("That name is at position "+position)
def binarySearch(array, searchedValue):
begin = 0
end = len(array) - 1
position = -1
found = False
while !=found & begin<=end:
middle=(begin+end)/2
if array[middle]== searchedValue:
found=True
position = middle
elif array[middle] >value:
end=middle-1
else:
first =middle+1
return position
采纳答案by Martijn Pieters
Your editor replaced '
(ASCII 39) with U+2018 LEFT SINGLE QUOTATION MARKcharacters, usually a sign you used Word or a similar wordprocessor instead of a plain text editor; a word processor tries to make your text 'prettier' and auto-replaces things like simple quotes with fancy ones. This was then saved in the Windows 1252 codepage encoding, where the fancy quotes were saved as hex 91 characters.
您的编辑器将'
(ASCII 39)替换为U+2018 LEFT SINGLE QUOTATION MARK字符,通常是您使用 Word 或类似文字处理器而不是纯文本编辑器的标志;文字处理器试图使您的文本“更漂亮”,并自动用花哨的引号替换简单的引号之类的内容。然后将其保存在Windows 1252 代码页编码中,其中花哨的引号保存为 16 进制 91 字符。
Python is having none of it. It wants source code saved in UTF-8 and using '
or "
for quotation marks. Use notepad, or better still, IDLE to edit your Python code instead.
Python 一无所有。它希望以 UTF-8 格式保存源代码并使用'
或"
作为引号。使用记事本或更好的 IDLE 来编辑您的 Python 代码。
You have numerous other errors in your code; you cannot use spaces in your variable names, for example, and Python uses and
, not &
as the boolean AND operator. !=
is an operator requiring 2 operands (it means 'not equal', the opposite of ==
), the boolean NOT operator is called not
.
您的代码中还有许多其他错误;例如,您不能在变量名中使用空格,并且 Python 使用and
, 而不是&
作为布尔 AND 运算符。!=
是一个需要 2 个操作数的运算符(它的意思是“不等于”,与 的相反==
),布尔非运算符被称为not
。
回答by u7278937
Add this line at the top of you code. It may work.
在代码顶部添加此行。它可能会起作用。
# coding=utf8
回答by agcala
The character you are beginning your constant strings with is not the right string delimiter.You are using
您开始常量字符串的字符不是正确的字符串分隔符。您正在使用
‘Ava Fischer' # ‘ and ' as string delimiters
when it should have been either
当它应该是
'Ava Fischer' # Ascii 39 as string delimiter
or maybe
或者可能
"Ava Fischer" # Ascii 34 as string delimiter
回答by Michael Kiros
If you're using Notepad++, click Encoding
at the top and choose Encode in UTF-8
.
如果您使用的是 Notepad++,请单击Encoding
顶部的 并选择Encode in UTF-8
。