1.问题
Traceback (most recent call last): File “/Users/xtransfer/Mike/01_workspace/01_python_workspace/01_prod_branch_workspace/atta-ai/cv/src/process/edm_mail_content_audit_demo3.py”, line 754, in print(‘是否是中文:’, ‘债’.decode(‘utf-8’) >= u’/u4e00’ and ‘债’.decode(‘utf-8’) <= u’/u9fa5’) AttributeError: ‘str’ object has no attribute ‘decode’ AttributeError: ‘str‘ object has no attribute ‘decode‘解决方法 https://blog.csdn.net/qq_43192819/article/details/108981008
2.原因
- 1.python2和python3在字符串编码上的区别;
- 2.Python 3.4: str : AttributeError: ‘str’ object has no attribute 'decode
针对解决方案
-
1.原因一的解决方法: print (‘张俊’.encode(‘utf-8’). decode(‘utf-8’) ) #必须将字节字符串解码后才能打印出来 参考链接:https://www.cnblogs.com/geekard/archive/2012/10/04/python-string-endec.html -
2.原因二的解决方法:各种编码方式尝试解决:utf-8,gbk,ISO-8859-1,gb2312
**** 结论:原因1才是主要原因,与编码集无关
3.问题复现
1.python2
result = str('中').decode('utf-8')
print('是否是中文:', result >= u'\u4e00' and result <= u'\u9fff')
python环境: 结果
File "/Users/demo11.py", line 3
SyntaxError: Non-ASCII character '\xe6' in file /Users/demo11.py on line 3, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details
结论:python2中虽然有decode方法,但也不能这么使用
2.python3
result = str('债').decode('utf-8')
print('是否是中文:', result >= u'\u4e00' and result <= u'\u9fff')
python环境: 结果
Traceback (most recent call last):
File "/Users/demo3.py", line 965, in <module>
result = str('债').decode('utf-8')
AttributeError: 'str' object has no attribute 'decode'
4.解决方案
在python3中,先进行encode,再decode,才能将字符转换成unicode编码
class DecodeTestDemo:
def __init__(self):
pass
def is_not_en_word(self, word:str):
'''
判断一个词是否是非英文词,只要包含一个中文,就认为是非英文词汇
:param word:
:return:
'''
count = 0
for s in word.encode('utf-8').decode('utf-8'):
if u'\u4e00' <= s <= u'\u9fff':
count += 1
break
if count > 0:
return True
else:
return False
if __name__ == '__main__':
print(uchar >= u'/u4e00' and uchar <= u'/u9fa5')
result = '债'.encode('utf-8').decode('utf-8')
print('是否是中文:', result >= u'\u4e00' and result <= u'\u9fff')
decodeTestDemo = DecodeTestDemo()
print('sex:', decodeTestDemo.is_not_en_word('sex'))
print('sex啦:', decodeTestDemo.is_not_en_word('sex啦'))
print('啦啦啦:', decodeTestDemo.is_not_en_word('啦啦啦'))
5.知识扩展
1.python中的decode方法
2.python中的编码集
|