特殊字符串在类和控制台中的操作不同

发布于 2024-12-27 16:31:56 字数 787 浏览 6 评论 0原文

我输入

config = {}
config['x'] = ["vision","unknown","SCH-i569","harmony","中国移动"]

没问题python控制台可以处理它

但是当我在类init函数中编写相同的代码时,它会出现一些编码问题,

class Profile(object):
def __init__(self):
    attributes = []
    attributes.append('8')
    attributes.append('htc')
    config = {}
    config['x'] = ["vision","unknown","SCH-i569","harmony","中国移动"]
    print config['x']
    attributes.append(config['x'])
    self.attributes = attributes
def getAttributes(self):
    return self.attributes

它会出现一个问题:

File "<stdin>", line 7
SyntaxError: (unicode error) 'utf8' codec can't decode byte 0xd6 in position 0: invalid continuation byte

当我删除“中国移动”时,类工作。我不知道为什么一个班级和其他班级之间有不同

I input

config = {}
config['x'] = ["vision","unknown","SCH-i569","harmony","中国移动"]

is ok. The python console can handle it

But when I write the same code in a class init function,it occurs some encode problems

class Profile(object):
def __init__(self):
    attributes = []
    attributes.append('8')
    attributes.append('htc')
    config = {}
    config['x'] = ["vision","unknown","SCH-i569","harmony","中国移动"]
    print config['x']
    attributes.append(config['x'])
    self.attributes = attributes
def getAttributes(self):
    return self.attributes

it occurs a problem:

File "<stdin>", line 7
SyntaxError: (unicode error) 'utf8' codec can't decode byte 0xd6 in position 0: invalid continuation byte

when I delete the "中国移动", the class work. I don't know why it is different between a class and other

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我做我的改变 2025-01-03 16:31:56

#-*-coding:utf-8-*- 添加到源文件中。

http://www.python.org/dev/peps/pep-0263/

Add #-*-coding:utf-8-*- to your source file.

http://www.python.org/dev/peps/pep-0263/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文