语法错误:当函数返回“£"时,文件中的非 ASCII 字符“xa3" [英] SyntaxError: Non-ASCII character 'xa3' in file when function returns '£'

查看:30
本文介绍了语法错误:当函数返回“£"时,文件中的非 ASCII 字符“xa3"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有一个函数:

def NewFunction():
    return '£'

我想打印一些前面有井号的东西,当我尝试运行这个程序时它打印了一个错误,显示了这个错误信息:

I want to print some stuff with a pound sign in front of it and it prints an error when I try to run this program, this error message is displayed:

SyntaxError: Non-ASCII character 'xa3' in file 'blah' but no encoding declared;
see http://www.python.org/peps/pep-0263.html for details

谁能告诉我如何在返回函数中包含井号?我基本上是在一个类中使用它,它在 '__str__' 部分中,包含了井号.

Can anyone inform me how I can include a pound sign in my return function? I'm basically using it in a class and it's within the '__str__' part that the pound sign is included.

推荐答案

我建议您阅读错误给您的 PEP.问题是您的代码试图使用 ASCII 编码,但井号不是 ASCII 字符.尝试使用 UTF-8 编码.您可以首先将 # -*- coding: utf-8 -*- 放在 .py 文件的顶部.为了更高级,您还可以在代码中逐个字符串地定义编码.但是,如果您尝试将井号文字放入您的代码中,您将需要一个支持整个文件的编码.

I'd recommend reading that PEP the error gives you. The problem is that your code is trying to use the ASCII encoding, but the pound symbol is not an ASCII character. Try using UTF-8 encoding. You can start by putting # -*- coding: utf-8 -*- at the top of your .py file. To get more advanced, you can also define encodings on a string by string basis in your code. However, if you are trying to put the pound sign literal in to your code, you'll need an encoding that supports it for the entire file.

这篇关于语法错误:当函数返回“£"时,文件中的非 ASCII 字符“xa3"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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