Python,用短划线替换长划线? [英] Python, replace long dash with short dash?

查看:58
本文介绍了Python,用短划线替换长划线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用短划线 (-) 替换长划线 ().我的代码:

I want to replace a long dash () with a short dash (-). My code:

if " – " in string:
      string = string.replace(" – ", " - ")

导致以下错误:

语法错误:第 76 行文件 ./script.py 中的非 ASCII 字符 '\xe2',但未声明编码;见 http://www.python.org/peps/pep-0263.html 详情

SyntaxError: Non-ASCII character '\xe2' in file ./script.py on line 76, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details

我该如何解决这个问题?

How can I fix this?

推荐答案

长破折号不是 ASCII 字符.声明你的脚本的编码,像这样(某处):

Long dash is not an ASCII character. Declare encoding of your script, like this (somewhere on top):

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

除了 utf-8 之外还有其他编码,但如果不使用涵盖几乎所有(unicode)字符的 ASCII 字符,使用 utf-8 总是安全的.

There are also other encodings beside utf-8 but it is always safe to use utf-8 if not working with ASCII characters which covers virtually all (unicode) characters.

有关详细信息,请参阅 PEP 0263.

See PEP 0263 for more info.

这篇关于Python,用短划线替换长划线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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