Python“语法错误:文件中的非ASCII字符'\ xe2'”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21639275/
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
Python "SyntaxError: Non-ASCII character '\xe2' in file"
提问by KDecker
I am writing some python code and I am receiving the error message as in the title, from searching this has to do with the character set.
我正在编写一些 python 代码,我收到标题中的错误消息,搜索这与字符集有关。
Here is the line that causes the error
这是导致错误的行
hc = HealthCheck("instance_health", interval=15, target808="HTTP:8080/index.html")
I cannot figure out what character is not in the ANSI ASCII set? Furthermore searching "\xe2" does not give anymore information as to what character that appears as. Which character in that line is causing the issue?
我不知道什么字符不在 ANSI ASCII 集中?此外,搜索 "\xe2" 不再提供有关出现的字符的信息。该行中的哪个字符导致了问题?
I have also seen a few fixes for this issue but I am not sure which to use. Could someone clarify what the issue is (python doesn't interpret unicode unless told to do so?), and how I would clear it up properly?
我也看到了一些针对此问题的修复,但我不确定要使用哪个。有人可以澄清问题是什么(除非被告知,否则python不会解释unicode?),以及我如何正确清除它?
EDIT: Here are all the lines near the one that errors
编辑:这是错误附近的所有行
def createLoadBalancer():
conn = ELBConnection(creds.awsAccessKey, creds.awsSecretKey)
hc = HealthCheck("instance_health", interval=15, target808="HTTP:8080/index.html")
lb = conn.create_load_balancer('my_lb', ['us-east-1a', 'us-east-1b'],[(80, 8080, 'http'), (443, 8443, 'tcp')])
lb.configure_health_check(hc)
return lb
采纳答案by DSM
You've got a stray byte floating around. You can find it by running
你有一个散落的字节。你可以通过运行找到它
with open("x.py") as fp:
for i, line in enumerate(fp):
if "\xe2" in line:
print i, repr(line)
where you should replace "x.py"by the name of your program. You'll see the line number and the offending line(s). For example, after inserting that byte arbitrarily, I got:
你应该用"x.py"你的程序名称替换的地方。您将看到行号和违规行。例如,在任意插入该字节后,我得到:
4 "\xe2 lb = conn.create_load_balancer('my_lb', ['us-east-1a', 'us-east-1b'],[(80, 8080, 'http'), (443, 8443, 'tcp')])\n"
回答by Cam
When I have a similar issue when reading text files i use...
当我在阅读文本文件时遇到类似问题时,我使用...
f = open('file','rt', errors='ignore')
回答by Chris Redford
If you are just trying to use UTF-8 characters or don't care if they are in your code, add this line to the top of your .pyfile
如果您只是尝试使用 UTF-8 字符或不关心它们是否在您的代码中,请将此行添加到.py文件顶部
# -*- coding: utf-8 -*-
回答by Kat Russo
I got this error for characters in my comments (from copying/pasting content from the web into my editor for note-taking purposes).
我的评论中的字符出现了这个错误(从网络复制/粘贴内容到我的编辑器以做笔记)。
To resolve in Text Wrangler:
要在 Text Wrangler 中解决:
- Highlight the text
- Go the the Text menu
- Select "Convert to ASCII"
- 突出显示文本
- 转到文本菜单
- 选择“转换为 ASCII”
回答by nagrom
I had this exact issue running the simple .py code below:
我在运行下面的简单 .py 代码时遇到了这个确切的问题:
import sys
print 'version is:', sys.version
DSM's code above provided the following:
上面 DSM 的代码提供了以下内容:
1 'print \xe2\x80\x98version is\xe2\x80\x99, sys.version'
1 '打印\xe2\x80\x98version is\xe2\x80\x99, sys.version'
So the issue was that my text editor used SMART QUOTES, as John Y suggested. After changing the text editor settings and re-opening/saving the file, it works just fine.
所以问题是我的文本编辑器使用了 SMART QUOTES,正如 John Y 建议的那样。更改文本编辑器设置并重新打开/保存文件后,它工作正常。
回答by Dadaso Zanzane
Change the file character encoding,
更改文件字符编码,
put below line to top of your code always
始终将下面的行放在代码的顶部
# -*- coding: utf-8 -*-
回答by khalid sookia
I had the same error while copying and pasting a comment from the web
我在复制和粘贴来自网络的评论时遇到了同样的错误
For me it was a single quote (') in the word
对我来说,它是单词中的单引号 (')
I just erased it and re-typed it.
我只是删除它并重新输入它。
回答by caot
Based on PEP 0263 -- Defining Python Source Code Encodings
Python will default to ASCII as standard encoding if no other
encoding hints are given.
To define a source code encoding, a magic comment must
be placed into the source files either as first or second
line in the file, such as:
# coding=<encoding name>
or (using formats recognized by popular editors)
#!/usr/bin/python
# -*- coding: <encoding name> -*-
or
#!/usr/bin/python
# vim: set fileencoding=<encoding name> :
回答by Chris
I am trying to parse that weird windows apostraphe and after trying several things here is the code snippet that works.
我正在尝试解析那个奇怪的 Windows 撇号,在尝试了几件事之后,这里是有效的代码片段。
def convert_freaking_apostrophe(self,string):
try:
issuer_rename = string.decode('windows-1252')
except:
issuer_rename = string.decode('latin-1')
issuer_rename = issuer_rename.replace(u''', u"'")
issuer_rename = issuer_rename.encode('ascii','ignore')
try:
os.rename(directory+"/"+issuer,directory+"/"+issuer_rename)
print "Successfully renamed "+issuer+" to "+issuer_rename
return issuer_rename
except:
pass
#HANDLING FOR FUNKY APOSTRAPHE
if re.search(r"([\x90-\xff])", issuer):
issuer = self.convert_freaking_apostrophe(issuer)
回答by Mark Austin
After about a half hour of looking through stack overflow, It dawned on me that if the use of a single quote " ' " in a comment will through the error:
在查看堆栈溢出大约半小时后,我突然意识到如果在注释中使用单引号“'”将通过错误:
SyntaxError: Non-ASCII character '\xe2' in file
After looking at the traceback i was able to locate the single quote used in my comment.
查看回溯后,我能够找到我的评论中使用的单引号。

