Python“SyntaxError:文件中的非ASCII字符'\xe2'" [英] Python "SyntaxError: Non-ASCII character '\xe2' in file"

查看:39
本文介绍了Python“SyntaxError:文件中的非ASCII字符'\xe2'"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一些 python 代码,但我收到了标题中的错误消息,因为搜索这与字符集有关.

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.

这是导致错误的行

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

我不知道哪个字符不在 ANSI ASCII 集中?此外,搜索 "\xe2" 不再提供有关出现的字符的信息.该行中的哪个字符导致了问题?

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?

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

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?

这是错误附近的所有行

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

推荐答案

你有一个散落的字节.你可以通过运行找到它

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)

您应该用程序名称替换 "x.py" 的位置.您将看到行号和违规行.例如,在任意插入该字节后,我得到:

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"

这篇关于Python“SyntaxError:文件中的非ASCII字符'\xe2'"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆