Python在往文件里写东西的时候,如果ascii码报错

参考

http://stackoverflow.com/questions/19833440/unicodeencodeerror-ascii-codec-cant-encode-character-u-xe9-in-position-7

You need to encode Unicode explicitly before writing to a file, otherwise Python does it for you with the default ASCII codec.

Pick an encoding and stick with it:

f.write(printinfo.encode('utf8') + '\n')

UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 7: ordinal not in range(128) [duplicate]的更多相关文章

  1. 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: ...

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

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

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

    环境win10+anaconda2 在安装labelme时遇到了这个问题,其实跟labelme没啥关系,主要是python2读取中文路径时报错,因为默认编码是ASCII,不认识中文,看到有一个一次性解 ...

  5. 【Python】解决UnicodeEncodeError: 'ascii' codec can't encode character u'\u2019' in position 0: ordinal not in range(128)

    指定文件字符集为utf-8 在文件头部加入以下代码: import sys reload(sys) sys.setdefaultencoding('utf-8')

  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. 数据库连接 UnicodeEncodeError: 'latin-1' codec can't encode character u'\u4eac' in position 0: ordinal not in range(256),UnicodeEncodeError: 'latin-1' codec can't encode character

    处理方法就是,设置连接和游标的charset为你所希望的编码,如utf8, 连接数据库的时候加上   charset="utf8 db = MySQLdb.connect(host= '14 ...

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

  9. 解决:"UnicodeEncodeError: 'ascii' codec can't encode character u'\xa0' in position"错误

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/Haiyang_Duan/article/ ...

随机推荐

  1. mysql学习之旅

    1,mysql安装脚本 #!/bin/bash RSYNC='10.39.2.120::wenhui/database/install' CMAKE='cmake-2.8.6.tar.gz' MYSQ ...

  2. 033——VUE中安装使用vue-devtools调试工具用于监控数据变化

    vue官网:https://cn.vuejs.org/ 下的官方仓库:vue-devtools  安装到火狐或谷歌下都可以,安装成功之后,按F12查看就可以了

  3. querySelectorAll 与jquery.find 与htmlcollection 的区别

    querySelector 和 querySelectorAll 规范定义 querySelector 和 querySelectorAll 方法是 W3C Selectors API Level 1 ...

  4. 【jQuery】IE9 jQuery 1.9.1 报 Syntax error,unrecognized expression 错误

    <script type="ctrip-template-x" id="ctrip-page-index"> <article class=& ...

  5. ArcGIS中标注转注记方法比较

    [数据处理]ArcGIS中标注转注记方法比较 (2013-02-22 08:42:15) 转载▼ 标签: arcgis 标注 注记 label annotation 分类: 数据处理 1.概述 由于切 ...

  6. Ubuntu 14.10安装simplescalar

    经过两天的安装,终于把simplescalar安装成功,安装过程中在网上找了很多资料,但是很多都是关于低版本ubuntu安装的过程,而且按照这些教程也没有安装成功,来记录一下安装中出现的问题及解决方案 ...

  7. 简单说说什么是Restful

    在确定要把自己的服务创建成RESTFUL之前,要明白什么样的服务什么是RESTFUL service(https://en.wikipedia.org/wiki/Representational_st ...

  8. wordpress 生成自定义 meta box

    工具 https://jeremyhixon.com/tool/wordpress-meta-box-generator/ 使用 生成代码 /** * Generated by the WordPre ...

  9. linux环境搭建记录

    第一次搭建环境,部署服务,在此记录一下过程 1.项目用到的hosts设置好 2.mkdir data,在data文件夹下建server,log,soft,resource路径,上载jdk.zip到so ...

  10. 升级到XE10

    下午抽空从XE7升级到XE10,用的是lsuper大侠的Dx10Update1_23.0.21418.4207,用到的控件基本装全乎了. 过程中也碰到点问题,记录下子. 1. cnPack结构匹配线与 ...