【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') 重新运行,不再报错
随机推荐
- event.keycode值大全
window.event.keycode-获取按下的键盘值 event.keycode值大全1 keycode 8 = BackSpace BackSpace 2 keycode 9 = Tab ...
- hdu 1284 钱币兑换问题(动态规划)
Ac code : 完全背包: #include<stdio.h> #include<string.h> int dp[4][40000]; int main(void) { ...
- PHP中is_numeric函数十六进制绕过0day
0×00 简介国内一部分CMS程序里面有用到过is_numberic函数,我们先看看这个函数的结构bool is_numeric ( mixed $var )如果 var 是数字和数字字符串则返回 T ...
- 转:Java NIO系列教程(四) Scatter/Gather
Java NIO开始支持scatter/gather,scatter/gather用于描述从Channel(译者注:Channel在中文经常翻译为通道)中读取或者写入到Channel的操作.分散(sc ...
- C++构造函数详解及显式调用构造函数
来源:http://www.cnblogs.com/xkfz007/archive/2012/05/11/2496447.html c++类的构造函数详解 ...
- 【原创】express3.4.8源码解析之路由中间件
前言 注意:旧文章转成markdown格式. 跟大家聊一个中间件,叫做路由中间件,它并非是connect中内置的中间件,而是在express中集成进去的. 显而易见,该中间件的用途就是 ------ ...
- 移动端富文本编辑器artEditor
摘要: 由于手机上打字比较慢,并不适合长篇大论的文章,所以移动端的富文本编辑器很少.artEditor是一款基于jQuery的移动端富文本编辑器,支持插入图片,后续完善其他功能. 插件地址:https ...
- IIS计数器
Bytes Total/sec 是 Bytes Sent/sec 与 Bytes Received/sec 的总和.这是 Web 服务每秒传输的总字节数. Cache Total Turnover R ...
- 表单元素的写法及与后台php的交互
1.<select class="textEnaSty" name="Port" size="1" onchange="Ob ...
- [转]linux awk命令详解
原文链接 : http://blog.chinaunix.net/uid-23302288-id-3785105.html awk是行处理器: 相比较屏幕处理的优点,在处理庞大文件时不会出现内存溢 ...