在python2.7下,因为想从数据库中读出来分类名进行写入到文件,提示

Traceback (most recent call last):
  File "test.py", line 28, in <module>
    fp.write("%d:%s\r\n"%(sClassid,sClassName))
UnicodeEncodeError: 'ascii' codec can't encode character u'\uff08' in position 12: ordinal not in range(128)

不用fp.write,用print打印却正常,这到底是怎么回来呢?

#! /usr/bin/python
# -*- coding: utf-8 -*-
import sys
print sys.getdefaultencoding();

运行上面的程序提示

ascii

原来如此,在程序的头部加上

import sys

reload(sys)
sys.setdefaultencoding('utf-8')

再次运行,错误消息。

总结一下,python2.7是基于ascii去处理字符流,当字符流不属于ascii范围内,就会抛出异常(ordinal not in range(128)。

UnicodeEncodeError: 'ascii' codec can't encode character...的解决方法的更多相关文章

  1. Python 排错UnicodeEncodeError 'ascii' codec can't encode character 错误解决方法

    Python UnicodeEncodeError 'ascii' codec can't encode character 错误解决方法   by:授客 QQ:1033553122 错误描述: py ...

  2. Windows下pip安装及更新出现“UnicodeEncodeError: 'ascii' codec can't encode character u'\u258c' in position 8: ordinal not in range(128)”问题解决办法

    Windows下pip安装及更新出现“UnicodeEncodeError: 'ascii' codec can't encode character u'\u258c' in position 8: ...

  3. UnicodeEncodeError: 'ascii' codec can't encode character u'\u5728' in position 1

    s = "图片picture"print chardet.detect(s) for c in s.decode('utf-8'): print c UnicodeEncodeEr ...

  4. UnicodeEncodeError: 'ascii' codec can't encode character u'\u5929' in position 2: ordinal not in range(128)

    UnicodeEncodeError: 'ascii' codec can't encode character u'\u5929' in position 2: ordinal not in ran ...

  5. UnicodeEncodeError: 'ascii' codec can't encode character u'\u65e0' in position 1: ordinal not in range(128)

    UnicodeEncodeError: 'ascii' codec can't encode character u'\u65e0' in position 1: ordinal not in ran ...

  6. UnicodeEncodeError: ‘ascii’ codec can’t encode character u’\u8888′ in position 0: ordinal not in range(168)

    python保存文件UnicodeEncodeError以及reload(sys)后print失效问题 在将字符串写入文件时,执行f.write(str),后台总是报错:UnicodeEncodeEr ...

  7. Upgrade to 17.1 from 17.0 problem:UnicodeEncodeError: 'ascii' codec can't encode character '\xc4' in position 50: ordinal not in range(128)

    最近 gentoo 从 17.0 更新到 17.1, 需要手动进行升级配置,使用 unsymlink-lib -p --finish 这一步的时候报错,报错如下: /usr/lib/python-ex ...

  8. python2.7 的中文编码处理,解决UnicodeEncodeError: 'ascii' codec can't encode character 问题

    最近业务中需要用 Python 写一些脚本.尽管脚本的交互只是命令行 + 日志输出,但是为了让界面友好些,我还是决定用中文输出日志信息. 很快,我就遇到了异常: UnicodeEncodeError: ...

  9. Python的Ftplib问题:UnicodeEncodeError: 'latin-1' codec can't encode characters的解决方法

    ftplib中有一个方法是cwd,用来切换目录,需要传入一个dirname,经过个人测试,该dirname不能含有汉字,会抛出:UnicodeEncodeError: 'latin-1' codec ...

随机推荐

  1. Paxos 算法

    1 概述 Paxos算法是莱斯利·兰伯特(Leslie Lamport,就是 LaTeX 中的"La",此人现在在微软研究院)于1990年提出的一种基于消息传递的一致性算法.[1] ...

  2. java--jdk api中其他对象(System,Runtime,Calendar,Math,Random,Date)

    转载请申明出处:http://blog.csdn.net/xmxkf/article/details/9796729 day18-01-其他对象(System) SystemDemo java.lan ...

  3. OS X 平台的 8 个实用终端工具

    本文由 伯乐在线 - shinancao 翻译自 mitchchn.欢迎加入iOS小组.转载请参见文章末尾处的要求. OS X 终端对外开放了许多很强大的UNIX实用工具和脚本.如果你是从Linux转 ...

  4. 抛开rails使用ActiveRecord连接数据库

    今天是大年三十,明天就正式进入羊年鸟,给所有程序猿(媛)同人拜个年吧!祝大家身体健康,事业有成,财源广进哦! 话归正题,以前都是在rails中使用数据库,或者在rails的console中使用:我们如 ...

  5. Android studio 项目(Project)依赖(非Module)

    Android studio 项目(Project)依赖(非Module) 0. 前言 对于Module 级别的依赖大家都知道,今天说下Android Studio下的项目依赖. 场景: A Proj ...

  6. twisted高并发库transport函数处理数据包的些许问题

    还是在学校时间比较多, 能够把时间更多的花在学习上, 尽管工作对人的提升更大, 但是总是没什么时间学习, 而且工作的气氛总是很紧凑, 忙碌, 少了些许激情吧.适应就好了.延续着之前对twisted高并 ...

  7. sql——查询出表中不为空或为空字段的总值数

    查询所给的表中值为空的总数 判断字段是否为空的sql语句 SELECT sex FROM id where sex is not NULL SELECT COUNT(*) t FROM id wher ...

  8. two sum II

    Given an array of integers that is already sorted in ascending order, find two numbers such that the ...

  9. Mybatis 系列7

    上篇系列6中 简单地给mybatis的配置画上了一个句号.那么从本篇文章开始,将会介绍mapper映射文件的配置. 这是mybatis的核心之一 一定要学好 在mapper文件中,以mapper作为根 ...

  10. MySql 时间操作(今天,昨天,7天,30天,本月,上月)

    1 . 查看当天日期 select current_date(); 2. 查看当天时间 select current_time(); 3.查看当天时间日期 select current_timesta ...