【Python】【解决】UnicodeDecodeError: 'ascii' codec can't decode byte 0xe5 in position 1: ordinal not in range(128)
1、问题描述
今天在升级Ubuntu到14.04,使用命令行启动软件更新器,进行版本升级,结果开始升级就异常退出了,具体打印如下:
$update-manager -d
正在检查新版 Ubuntu
使用 'trusty.tar.gz.gpg' 对 'trusty.tar.gz' 进行验证
正在提取 'trusty.tar.gz'
Traceback (most recent call last):
File "/tmp/ubuntu-release-upgrader-r_0oij/trusty", line , in <module>
sys.exit(main())
File "/tmp/ubuntu-release-upgrader-r_0oij/DistUpgrade/DistUpgradeMain.py", line , in main
if app.run():
File "/tmp/ubuntu-release-upgrader-r_0oij/DistUpgrade/DistUpgradeController.py", line , in run
return self.fullUpgrade()
File "/tmp/ubuntu-release-upgrader-r_0oij/DistUpgrade/DistUpgradeController.py", line , in fullUpgrade
if not self.updateSourcesList():
File "/tmp/ubuntu-release-upgrader-r_0oij/DistUpgrade/DistUpgradeController.py", line , in updateSourcesList
if not self.rewriteSourcesList(mirror_check=True):
File "/tmp/ubuntu-release-upgrader-r_0oij/DistUpgrade/DistUpgradeController.py", line , in rewriteSourcesList
logging.debug("entry '%s' was disabled (unknown mirror)" % get_string_with_no_auth_from_source_entry(entry))
File "/tmp/ubuntu-release-upgrader-r_0oij/DistUpgrade/utils.py", line , in get_string_with_no_auth_from_source_entry
return str(tmp)
File "/tmp/ubuntu-release-upgrader-r_0oij/DistUpgrade/sourceslist.py", line , in __str__
return self.str().strip()
File "/tmp/ubuntu-release-upgrader-r_0oij/DistUpgrade/sourceslist.py", line , in str
line += u" #%s" % self.comment
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe5 in position 1: ordinal not in range(128)
2、方案探索
主要错误是上面最后一行的Unicode解码问题,网上搜索说是读取文件时使用的编码默认时ascii而不是utf8,导致的错误;原始资料路径为:
http://blog.csdn.net/vincent_czz/article/details/7719012
在代码中加上如下几句即可。
import sys
reload(sys)
sys.setdefaultencoding('utf8')
http://docs.python.org/howto/unicode.html 这个是python的unicode编码API文档,英文好的同学可以看一下,加深理解。
3、解决方案
3.1 拷贝临时数据到本地
根据上面错误提示,拷贝升级包代码到本地,并修改权限为自己可以编辑
$sudo cp -R /tmp/ubuntu-release-upgrader-r_0oij ./upgrade
$sudo chown qunengrong:qunengrong upgrade/ -R
3.2 修正读取文件的代码
打开上面报错的文件,如DistUpgradeMain.py,进行编辑并保存;
## 原来为下面行
#import sys
## 改为下面的3行
import sys
reload(sys)
sys.setdefaultencoding('utf8')
3.3 手动启动升级
为了安全和防止升级过程被打断,建议提交到后台执行,如使用nohup,输入密码后按CTRL+Z终端,然后输入bg命令让其后台执行:
$nohup sudo ./trusty
nohup: 忽略输入并把输出追加到"nohup.out"
[sudo] password for qunengrong: ^Z
[]+ 已停止 nohup sudo ./trusty
qunengrong@qunengrong-Studio- ~/tests/apt/upgrade
$bg
[]+ nohup sudo ./trusty &
已经可以正常升级了,如下:

【Python】【解决】UnicodeDecodeError: 'ascii' codec can't decode byte 0xe5 in position 1: ordinal not in range(128)的更多相关文章
- 解决UnicodeDecodeError: ‘ascii’ codec can’t decode byte 0xe5 in position 108: ordinal not in range(128
今天做网页到了测试和数据库交互的地方,其中HTML和数据库都是设置成utf-8格式编码,插入到数据库中是正确的,但是当读取出来的时候就会出错,原因就是python的str默认是ascii编码,和uni ...
- HTMLTestRunner解决UnicodeDecodeError: ‘ascii’ codec can’t decode byte 0xe5 in position 108: ordinal not in range(128)
其中HTML和数据库都是设置成utf-8格式编码,插入到数据库中是正确的,但是当读取出来的时候就会出错,原因就是python的str默认是ascii编码,和unicode编码冲突,就会报这个标题错误. ...
- 解决UnicodeDecodeError: 'ascii' codec can't decode byte 0xe7 in position 12: ordinal not in range(128)的编码问题
当我在运行一个基于scrapy的爬虫时出现UnicodeDecodeError: 'ascii' codec can't decode byte 0xe7 in position 12: ordina ...
- UnicodeDecodeError: 'ascii' codec can't decode byte 0xe5 in position 0: ordinal not in range(128)
问题分析:错误提示中的“ordinal not in range(128)”,意思是,字符不在128范围内,即说明不是普通的ASCII字符,超出处理能力了. import sys print u'系统 ...
- 解决UnicodeDecodeError: 'ascii' codec can't decode byte 0xcf in position 7: ordinal not in range(128)
在Windows下同时装了Python2和Python3,但是在使用命令给pip更新的时候,出现了以下错误: 解决办法:修改mimetypes.py文件,路径位于python的安装路径下的Lib\mi ...
- python 错误: UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 in position 0: ordinal not in range(128)
参照:http://www.runoob.com/django/django-form.html 做了个表单提交和回显,但是报了以上错误 查资料发现是 python从request取值的是unicod ...
- UnicodeDecodeError: 'ascii' codec can't decode byte 0xe5 in position 1: ordinal not in range(128)
对于写python的人来说,这个异常一点不陌生,只要涉及到中文字符串的操作,一不小心就会出错.关于这个问题的解释,找到一篇好文,分享一下. 原文地址:https://blog.csdn.net/u01 ...
- [错误处理]UnicodeDecodeError: 'ascii' codec can't decode byte 0xe5 in position 0: ordinal not in range(128)
Stackoverflow 回答: 将byte类型转化:byte_string.decode('utf-8') Jinja2 is using Unicode internally which mea ...
- UnicodeDecodeError:'ascii' codec can't decode byte 0xe5 in position 89: ordinal not in range(128)
环境python2,在出现该错误的python文件,增加: import sys reload(sys) sys.setdefaultencoding('utf8') 重新运行,不再报错
随机推荐
- hdu 1176 免费馅饼(动态规划)
AC code: #include<stdio.h> #include<string.h> #define max(a,b) (a>b?a:b) #define maxo ...
- C# Attribute 特性 学习
一.特性的概述 公共语言运行库允许您添加类似关键字的描述性声明(称为特性 (Attribute))来批注编程元素,如类型.字段.方法和属性 (Property).属性与 Microsoft .NET ...
- 斯坦福大学CS224d基础1:线性代数回顾
转自 http://blog.csdn.net/han_xiaoyang/article/details/51629242 斯坦福大学CS224d基础1:线性代数知识 作者:Zico Kolter ( ...
- php 获取后缀的几种方法
1: function get_extension($file){ substr(strrchr($file, '.'), 1); } 2: function get_extension($file) ...
- c/s架构nginx+php-fpm通信原理
FastCGI是一个运用于Http Server和动态脚本语言间通信的接口,多数流行的Http Server都支持FastCGI,包括Apache.Nginx和lighttpd等.同时,Fas ...
- win7打开网页老是提示下载网页解决办法
方法:我的系统是windows 7 旗舰版, 解决方法可以自己手动去修复,方法是进入命令窗口. 开始--运行--cmd--sfc.exe--sfc/scannow 修复一下!
- ProcDump
https://technet.microsoft.com/en-us/sysinternals/dd996900.aspx
- 用开源Look&Feel (Substance)写 漂亮的Swing应用程序
今天用Swing 做了一个模仿QQ2009的登录界面,用到了开源的Look&Feel (Substance),在使用的过程中遇到了一些问题,也学到了一些技巧.Substance (https: ...
- LNMP安装成功的界面
在ubuntu13.10上面安装一个lnmp集成环境. 下面是安装成功的界面. ===========================add nginx and php-fpm on startup ...
- jQuery1.11源码分析(6)-----jQuery结构总揽
(在看以下内容之前请先对原型链有一定的了解,比如:prototype是对象还是函数?) 在看jQuery的其他源码之前,必须对jQuery的数据结构有一定的了解. jQuery的核心很简单,jQuer ...