问题:Python“ SyntaxError:文件中的非ASCII字符’\ xe2’”

我正在编写一些python代码,并且从标题中收到错误消息,如标题所示,这与字符集有关。

这是导致错误的行

hc = HealthCheck("instance_health", interval=15, target808="HTTP:8080/index.html")

我无法确定ANSI ASCII字符集中没有哪个字符?此外,搜索“ \ xe2”不再提供有关显示为哪个字符的信息。该行中的哪个字符导致了问题?

我也看到了一些针对此问题的修复程序,但是我不确定该使用哪个修复程序。有人可以弄清楚问题是什么(除非被告知这样做,否则python不会解释unicode?),以及如何正确清除它?

编辑:这是错误的那一行附近的所有行

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

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.

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?

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?

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

回答 0

您有一个流浪字节浮动。您可以通过运行找到它

with open("x.py") as fp:
    for i, line in enumerate(fp):
        if "\xe2" in line:
            print i, repr(line)

在其中应替换"x.py"为程序名称。您会看到行号和有问题的行。例如,在任意插入该字节后,我得到:

4 "\xe2        lb = conn.create_load_balancer('my_lb', ['us-east-1a', 'us-east-1b'],[(80, 8080, 'http'), (443, 8443, 'tcp')])\n"

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:

4 "\xe2        lb = conn.create_load_balancer('my_lb', ['us-east-1a', 'us-east-1b'],[(80, 8080, 'http'), (443, 8443, 'tcp')])\n"

回答 1

如果您只是尝试使用UTF-8字符,或者不在乎代码中是否包含这些字符,请将此行添加到.py文件顶部

# -*- coding: utf-8 -*-

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 .py file

# -*- coding: utf-8 -*-

回答 2

或者您可以简单地使用:

# coding: utf-8

.py文件顶部

Or you could just simply use:

# coding: utf-8

at top of .py file


回答 3

\ xe2是’-‘字符,它出现在某些复制和粘贴过程中,它使用不同的等号’-‘导致编码错误。用正确的“-”(通过键盘按钮)替换“-”(来自复制粘贴)。

\xe2 is the ‘-‘ character, it appears in some copy and paste it uses a different equal looking ‘-‘ that causes encoding errors. Replace the ‘-‘(from copy paste) with the correct ‘-‘ (from you keyboard button).


回答 4

更改文件字符编码,

始终将下面的代码置于代码顶部

# -*- coding: utf-8 -*-

Change the file character encoding,

put below line to top of your code always

# -*- coding: utf-8 -*-

回答 5

从网络复制和粘贴评论时出现相同的错误

对我来说,这是一个单引号(’)

我只是将其擦除并重新输入。

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.


回答 6

在.py文件的第一行中添加#coding = utf-8行将解决此问题。

请在下面的链接中阅读有关该问题及其修复程序的更多信息,在本文中详细描述了该问题及其解决方案:https : //www.python.org/dev/peps/pep-0263/

Adding # coding=utf-8 line in first line of your .py file will fix the problem.

Please read more about the problem and its fix on below link, in this article problem and its solution is beautifully described : https://www.python.org/dev/peps/pep-0263/


回答 7

我在注释中遇到了字符错误(从网络上将内容复制/粘贴到编辑器中以做笔记)。

要在文本牧马人中解决:

  1. 突出显示文字
  2. 转到文本菜单
  3. 选择“转换为ASCII”

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:

  1. Highlight the text
  2. Go the the Text menu
  3. Select “Convert to ASCII”

回答 8

基于PEP 0263-定义Python源代码编码

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> :

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> :

回答 9

我遇到了同样的问题,只是将其添加到文件的顶部(在Python 3中,我没有问题,但在Python 2中有问题

#!/usr/local/bin/python
# coding: latin-1

I had the same issue and just added this to the top of my file (in Python 3 I didn’t have the problem but do in Python 2

#!/usr/local/bin/python
# coding: latin-1

回答 10

在查看堆栈溢出大约半小时之后,我突然意识到,如果在注释中使用单引号“’”将解决错误:

SyntaxError: Non-ASCII character '\xe2' in file

在查看了追溯之后,我能够找到我的注释中使用的单引号。

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.


回答 11

如果它对任何人都有用,那对我来说是发生了,因为我试图使用python 2.7命令在python 3.4中运行Django实现

If it helps anybody, for me that happened because I was trying to run a Django implementation in python 3.4 with my python 2.7 command


回答 12

我在运行以下简单的.py代码时遇到了这个确切的问题:

import sys
print 'version is:', sys.version

上面的DSM代码提供了以下内容:

1’print \ xe2 \ x80 \ x98version is \ xe2 \ x80 \ x99,sys.version’

因此,问题在于我的文本编辑器使用了John Qu所建议的SMART QUOTES。更改文本编辑器设置并重新打开/保存文件后,它可以正常工作。

I had this exact issue running the simple .py code below:

import sys
print 'version is:', sys.version

DSM’s code above provided the following:

1 ‘print \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.


回答 13

我正在尝试解析该奇怪的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)

I am trying to parse that weird windows apostraphe and after trying several things here is the code snippet that works.

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)

回答 14

我遇到了同样的问题,但这是因为我原样复制并粘贴了字符串。稍后,当我手动键入字符串时,错误消失了。

由于-标志,我出现了错误。当我用手动输入替换它时,-错误解决了。

复制的字符串 10 + 3 * 5/(16 − 4)

手动输入字符串 10 + 3 * 5/(16 - 4)

您可以清楚地看到两个连字符之间存在一些差异。

我认为这是因为不同的OS或不同的软件使用了不同的格式。

I had the same issue but it was because I copied and pasted the string as it is. Later when I manually typed the string as it is the error vanished.

I had the error due to the - sign. When I replaced it with manually inputting a - the error was solved.

Copied string 10 + 3 * 5/(16 − 4)

Manually typed string 10 + 3 * 5/(16 - 4)

you can clearly see there is a bit of difference between both the hyphens.

I think it’s because of the different formatting used by different OS or maybe just different software.


回答 15

对我来说,问题是由于引号中的符号“’”引起的。由于我已经从pdf文件复制了代码,因此导致了该错误。我只是用“”代替“”。

For me the problem had caused due to “’” that symbol in the quotes. As i had copied the code from a pdf file it caused that error. I just replaced “’” by this “‘”.


回答 16

如果要找出导致此问题的字符,只需将有问题的变量分配给字符串,然后在iPython控制台中将其打印出来。

就我而言

In [1]: array = [[24.9, 50.5]​, [11.2, 51.0]]        # Raises an error

In [2]: string = "[[24.9, 50.5]​, [11.2, 51.0]]"     # Manually paste the above array here

In [3]: string
Out [3]: '[[24.9, 50.5]\xe2\x80\x8b, [11.2, 51.0]]' # Here they are!

If you want to spot what character caused this just assign the problematic variable to a string and print it in a iPython console.

In my case

In [1]: array = [[24.9, 50.5]​, [11.2, 51.0]]        # Raises an error

In [2]: string = "[[24.9, 50.5]​, [11.2, 51.0]]"     # Manually paste the above array here

In [3]: string
Out [3]: '[[24.9, 50.5]\xe2\x80\x8b, [11.2, 51.0]]' # Here they are!

回答 17

对我来说,问题是由于在Mac Notes中键入代码,然后从Mac Notes复制代码,然后粘贴到我的vim会话中来创建文件而引起的。这使我的单引号成为弯曲类型。为了解决这个问题,我在vim中打开了我的文件,并通过删除和重新键入相同的字符,将所有弯曲的单引号替换为直线型。是Mac Notes使相同的按键产生弯曲的单引号。

for me, the problem was caused by typing my code into Mac Notes and then copied it from Mac Notes and pasted into my vim session to create my file. This made my single quotes the curved type. to fix it I opened my file in vim and replaced all my curved single quotes with the straight kind, just by removing and retyping the same character. It was Mac Notes that made the same key stroke produce the curved single quote.


回答 18

很长时间以来,我一直找不到问题所在,但后来我意识到我已经从Web复制了一行“ UTC-12:00”,而其中的连字符/破折号引起了问题。我只是再次写了这个“-”,问题就解决了。

因此,有时复制粘贴的行也会产生错误。在这种情况下,只需重写复制粘贴的代码即可。重写后,看起来什么都没有改变,但是错误消失了。

I was unable to find what’s the issue for long but later I realised that I had copied a line “UTC-12:00” from web and the hyphen/dash in this was causing the problem. I just wrote this “-” again and the problem got resolved.

So, sometimes the copy pasted lines also give errors. In such cases, just re-write the copy pasted code and it works. On re-writing, it would look like nothing got changed but the error will be gone.


回答 19

当我阅读文本文件时遇到类似问题时,我会使用…

f = open('file','rt', errors='ignore')

When I have a similar issue when reading text files i use…

f = open('file','rt', errors='ignore')

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。