- 报错:
UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal
- 解决:
这个报错是编码问题导致的
在文件头加上
# -*- coding:utf-8 -*-
对需要比较的中文字符变量如:
label = label.decode('utf-8')
具体实例:
- 问题展示:
[root@localhost .pyenv]# yum-config-manager –add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo 已加载插件:fastestmirror, langpacks /usr/lib/python2.7/site-packages/yum/misc.py:133: UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal return lambda s: s.lower() == pat
- 问题解决:
1 # -*- coding:utf-8 -*- 2 #! /usr/bin/python -tt 3 """ 4 Assorted utility functions for yum. 5 """ 6 7 import types 8 import os 9 import sys 10 import os.path 11 from cStringIO import StringIO 12 import base64 13 import binascii 14 import struct 15 import re 16 import errno 17 import Errors 18 import constants 19 import pgpmsg 20 import tempfile 21 import glob 22 import pwd 23 import fnmatch 24 import bz2 25 import gzip 26 import shutil 27 import urllib 28 import string ...... 124 def compile_pattern(pat, ignore_case=False): 125 """ Compile shell wildcards, return a 'match' function. """ 126 if re_glob(pat): 127 try: 128 flags = ignore_case and re.I or 0 129 return re.compile(fnmatch.translate(pat), flags).match 130 except re.error: 131 pass # fall back to exact match 132 if ignore_case: 133 pat = pat.lower() 134 return lambda s: s.lower() == pat.decode('utf-8') 135 return lambda s: s == pat
- 再次测试:
[root@localhost .pyenv]# yum-config-manager –add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo 已加载插件:fastestmirror, langpacks [root@localhost .pyenv]# yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo 已加载插件:fastestmirror, langpacks adding repo from: https://download.docker.com/linux/centos/docker-ce.repo grabbing file https://download.docker.com/linux/centos/docker-ce.repo to /etc/yum.repos.d/docker-ce.repo repo saved to /etc/yum.repos.d/docker-ce.repo [root@localhost .pyenv]#
分享一个有趣的 学习链接:https://xxetb.xet.tech/s/HY8za