Python 2.7:SyntaxError:非ASCII字符';0xe6';在文件中,错误不会消失

Python 2.7:SyntaxError:非ASCII字符';0xe6';在文件中,错误不会消失,python,utf-8,syntax-error,Python,Utf 8,Syntax Error,我有一个python文件,其中包含这行文本: 最好的汉字 土著儿童在家庭法诉讼中的利益。澳大利亚家庭法杂志12 140149 我试图处理该行,但它不断抛出错误: 语法错误:文件中的非ASCII字符“\xa3” 我已将此放在文件的顶部: #!/usr/bin/env python # -*- coding: utf-8 -*- 同样的错误也会发生 在保留上面的utf 8标头时,还尝试了以下操作: u'The best 汉字 interests of the Aboriginal child i

我有一个python文件,其中包含这行文本:

最好的汉字 土著儿童在家庭法诉讼中的利益。澳大利亚家庭法杂志12 140149

我试图处理该行,但它不断抛出错误:

语法错误:文件中的非ASCII字符“\xa3”

我已将此放在文件的顶部:

#!/usr/bin/env python
# -*- coding: utf-8 -*- 
同样的错误也会发生

在保留上面的utf 8标头时,还尝试了以下操作:

u'The best 汉字 interests of the Aboriginal child in family law proceedings. Australian Journal of Family Law  12  140149.'
unicode(The best 汉字 interests of the Aboriginal child in family law proceedings. Australian Journal of Family Law  12  140149.)
gettext.ugettext((The best 汉字 interests of the Aboriginal child in family law proceedings. Australian Journal of Family Law  12  140149.)
还是一样的错误

在保留上面的utf 8标头时,还尝试了以下操作:

u'The best 汉字 interests of the Aboriginal child in family law proceedings. Australian Journal of Family Law  12  140149.'
unicode(The best 汉字 interests of the Aboriginal child in family law proceedings. Australian Journal of Family Law  12  140149.)
gettext.ugettext((The best 汉字 interests of the Aboriginal child in family law proceedings. Australian Journal of Family Law  12  140149.)
还是一样的错误

在保留上面的utf 8标头时,还尝试了以下操作:

u'The best 汉字 interests of the Aboriginal child in family law proceedings. Australian Journal of Family Law  12  140149.'
unicode(The best 汉字 interests of the Aboriginal child in family law proceedings. Australian Journal of Family Law  12  140149.)
gettext.ugettext((The best 汉字 interests of the Aboriginal child in family law proceedings. Australian Journal of Family Law  12  140149.)
还是一样的错误

我错过了什么

以下是所有代码:

#!/usr/bin/env python
# -*- coding: utf-8 -*- 

import re

s = u'The best 汉字 interests of the Aboriginal child in family law 
proceedings. Australian Journal of Family Law  12  140149.'

def cleanup(s):
    control_chars = ''.join(map(unichr, range(0,9) + range(11,13) + range(14,32) + range(127,160)))
    cc_regex = re.compile('[%s]' % re.escape(control_chars))
    return cc_regex.sub(' ', s)

print cleanup(s)
回溯输出:

C:\EBI\Work>cd c:\EBI\Work && cmd /C "set "PYTHONIOENCODING=UTF-8" && set "PYTHONUNBUFFERED=1" && C:/Python27/python.exe 
Traceback (most recent call last):
  File "c:\EBI\Work\test2.py", line 7, in <module>
    s = unicode('The best µ▒ëσ¡ù interests of the Aboriginal child in family law proceedings. Australian Journal of Family Law  12  140☺149.')
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe6 in position 9: ordinal not in range(128)
C:\EBI\Work>cd C:\EBI\Work&&cmd/C“set”pythonionecoding=UTF-8“&&set”PYTHONUNBUFFERED=1“&&C:/Python27/python.exe
回溯(最近一次呼叫最后一次):
文件“c:\EBI\Work\test2.py”,第7行,在
s=unicode('最佳µ▒ëσù土著儿童在家庭法诉讼中的利益,《澳大利亚家庭法杂志》12 140☺149.')
UnicodeDecodeError:“ascii”编解码器无法解码第9位的字节0xe6:序号不在范围内(128)

您能否生成一个并显示完整的回溯?的UTF-8编码
汉字
不包含字节
A3
,因此您的源代码实际上不是用UTF-8编码的,或者语法错误是由其他原因造成的。添加了问题代码。第一件事:您应该使用Unicode文字作为文本:
u'best…'
,正如您所尝试的那样。这与您最初的问题不太匹配。在标题中,您的error消息抱怨字节
0xa3
,但现在是
0xe6
(这对汉字)。此外,请显示当前代码版本(带有
u'best…'
的版本)的回溯。是的,在这种情况下,您必须调整终端编码,正如我在前面的评论中所说。当您说“output tab”时,我想您指的是某个IDE,您可能需要在其中执行相同的操作(配置输出编码)。