Python, replace long dash with short dash?

断了今生、忘了曾经 提交于 2019-12-10 12:38:55

问题


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

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

results in the following error:

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?


回答1:


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

#-*- coding: 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.

See PEP 0263 for more info.




回答2:


I would like to link another answer: https://stackoverflow.com/a/42856932/3751268. However that only worked for Python 2.

Here is a solution for python 3:

my_str = '—asasas—'
my_str.replace(b'\xe2\x80\x94'.decode('utf-8'), '--')


来源:https://stackoverflow.com/questions/19149577/python-replace-long-dash-with-short-dash

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!