【问题标题】:Python 2.7: How to print this character?Python 2.7:如何打印这个字符?
【发布时间】:2020-08-07 14:10:31
【问题描述】:


我想在 python 2.7 中打印这个字符“¥”。 这是我的代码:

test = "¥"
print(test)

我执行此代码并收到以下错误消息:

SyntaxError: 第 5 行文件 main.py 中的非 ASCII 字符“\xc2”,但是 未声明编码;见http://python.org/dev/peps/pep-0263/ 详情

如何打印这个字符? 提前致谢

【问题讨论】:

标签: python python-2.7


【解决方案1】:

您需要在文件顶部指定以下行:

# -*- coding: <encoding> -*-

下次运行此文件时,Python 会知道该字符的编码是什么。

假设您使用的是 utf-8,它将看起来像这样。

# -*- coding: utf-8 -*-
test = "¥"
print(test)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-29
    • 2016-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-29
    • 2012-12-20
    • 2021-10-31
    相关资源
    最近更新 更多