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)的更多相关文章

  1. 解决UnicodeDecodeError: ‘ascii’ codec can’t decode byte 0xe5 in position 108: ordinal not in range(128

    今天做网页到了测试和数据库交互的地方,其中HTML和数据库都是设置成utf-8格式编码,插入到数据库中是正确的,但是当读取出来的时候就会出错,原因就是python的str默认是ascii编码,和uni ...

  2. HTMLTestRunner解决UnicodeDecodeError: ‘ascii’ codec can’t decode byte 0xe5 in position 108: ordinal not in range(128)

    其中HTML和数据库都是设置成utf-8格式编码,插入到数据库中是正确的,但是当读取出来的时候就会出错,原因就是python的str默认是ascii编码,和unicode编码冲突,就会报这个标题错误. ...

  3. 解决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 ...

  4. 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'系统 ...

  5. 解决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 ...

  6. 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 ...

  7. UnicodeDecodeError: 'ascii' codec can't decode byte 0xe5 in position 1: ordinal not in range(128)

    对于写python的人来说,这个异常一点不陌生,只要涉及到中文字符串的操作,一不小心就会出错.关于这个问题的解释,找到一篇好文,分享一下. 原文地址:https://blog.csdn.net/u01 ...

  8. [错误处理]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 ...

  9. 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') 重新运行,不再报错

随机推荐

  1. 关于updateElement接口

    1.bool UpdateGameElement(const struct_game_element& ele, gs_dbs_user_info_op_req& db_req, :: ...

  2. hdu 2048 神、上帝以及老天爷

    经典错排问题,算出n个人的排列可能,即求n!. 在本题中设定所有人即n个人全部拍错,即求n错排. 要求:求出其全部错排发生的概率 n错排 / n! * 100  以小数形式输出即可. #include ...

  3. gcc/g++ 参数

    -static  此选项将禁止使用动态库,所以,编译出来的东西,一般都很大,也不需要什么 动态连接库,就可以运行. -share  此选项将尽量使用动态库,所以生成文件比较小,但是需要系统由动态库.

  4. linux 下如何给用户添加权限

    linux 添加用户.权限:# useradd –d /usr/sam -m sam此命令创建了一个用户sam,其中-d和-m选项用来为登录名sam产生一个主目录/usr/sam(/usr为默认的用户 ...

  5. 《C++代码设计与重用》 书评

    作者:唐风 主页:www.cnblogs.com/liyiwen   前几个星期买了,一直没有直接细翻,买的时候看了背面的两个推荐,一个是孟岩,一个是Scott Meyers(Effective C+ ...

  6. logback logback.xml常用配置详解(三)

    logback logback.xml常用配置详解 <filter> <filter>: 过滤器,执行一个过滤器会有返回个枚举值,即DENY,NEUTRAL,ACCEPT其中之 ...

  7. __set(),__get() 魔术方法示例

    <?php class Ren{private $name;private $age;private $sex;function __set($name,$value){ if($name == ...

  8. CKEditor使用笔记

    相关资源 1. 首页地址:http://ckeditor.com/ 2. 下载地址:http://ckeditor.com/download 3. SDK地址:http://sdk.ckeditor. ...

  9. 5 个最受人喜爱的开源 Django 包

    导读 Django 围绕“可重用应用”的思想建立:自包含的包提供了可重复使用的特性.你可以将这些可重用应用组装起来,在加上适用于你的网站的特定代码,来搭建你自己的网站.Django 具有一个丰富多样的 ...

  10. L4 如何在XCode中下进行工作

    原地址:http://www.howzhi.com/course/286/lesson/2107 管理您的工作流程在Xcode 正如你在本教程中看到了  你的第一个iOS应用,你在Xcode工作区窗口 ...