【发布时间】:2016-07-09 15:50:18
【问题描述】:
我需要在 Python 中替换像 ¾ 这样的非 ASCII 字符,但我得到了
SyntaxError: Non-ASCII character '\xc2' in file test.py but no encoding declared; see http://www.python.org/peps/pep-0263.html for details`
按照on the webpage的指示后,我得到了
UnicodeDecodeError: 'ascii' codec can't decode byte 0xef in position 449: ordinal not in range(128)
这是我的代码:
data = data.replace(u"½", u"1/2")
data = re.sub(u"¾", u"3/4", data, flags=re.DOTALL)
我需要在我的代码中进行哪些更改?
我的文件是:
#!/usr/bin/python
with codecs.open("file.txt", "r", "utf8") as myfile:
data = myfile.read()
data = data.replace(u"½", u"1/2")
file.txt 是:
hello world ½
【问题讨论】:
-
@TusharGupta 这删除了字符 ...
-
如果可以去掉,可以用它来代替。试试看;)
-
我确实尝试了很多,这就是我问的原因,我现在没有想法
-
能否提供
data的内容(或一小部分)?
标签: python python-2.x