【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 10, in <module>

    sys.exit(main())

  File "/tmp/ubuntu-release-upgrader-r_0oij/DistUpgrade/DistUpgradeMain.py", line 243, in main

    if app.run():

  File "/tmp/ubuntu-release-upgrader-r_0oij/DistUpgrade/DistUpgradeController.py", line 1826, in run

    return self.fullUpgrade()

  File "/tmp/ubuntu-release-upgrader-r_0oij/DistUpgrade/DistUpgradeController.py", line 1717, in fullUpgrade

    if not self.updateSourcesList():

  File "/tmp/ubuntu-release-upgrader-r_0oij/DistUpgrade/DistUpgradeController.py", line 760, in updateSourcesList

    if not self.rewriteSourcesList(mirror_check=True):

  File "/tmp/ubuntu-release-upgrader-r_0oij/DistUpgrade/DistUpgradeController.py", line 736, 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 89, in get_string_with_no_auth_from_source_entry

    return str(tmp)

  File "/tmp/ubuntu-release-upgrader-r_0oij/DistUpgrade/sourceslist.py", line 232, in __str__

    return self.str().strip()

  File "/tmp/ubuntu-release-upgrader-r_0oij/DistUpgrade/sourceslist.py", line 256, 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

[1]+  已停止               nohup sudo ./trusty

qunengrong@qunengrong-Studio-1450 ~/tests/apt/upgrade

$bg

[1]+ nohup sudo ./trusty &

已经可以正常升级了,如下:

【Python】【解决】UnicodeDecodeError: 'ascii' codec can't decode byte 0xe5 in position 1: ordinal not in range(128)

 

 

你可能感兴趣的:(position)