zoukankan      html  css  js  c++  java
  • Python:解决中文字符串问题

    本人安装的是Python 2.7版本,由于编写程序的过程中会碰到中文字符串,但由于Python默认采用ASCII编码方式,所以对中文不支持。要解决此问题,必须设置当前编码方式为Unicode方式。

    默认ASCII编码方式对中文字符产生的异常为:UnicodeDecodeError: 'ascii' codec can't decode byte 0xe9 in position 0: ordinal not in range(128)

    解决方案:

    首先在文件头加入如下设置:

    #! /usr/bin/env python
     -*- coding: utf-8 -*-

    对需要 str->unicode 的代码,可以在前边写上

    import sys

    reload(sys)

    sys.setdefaultencoding('utf8')

    把 str 编码由 ascii 改为 utf8 (或 gb18030)

  • 相关阅读:
    NPM 重新回炉
    构建工具
    工作的环境部署
    Proxy 代理
    Promise 的 用法及实现
    JS 的 继承
    动态规划——LCS
    矩阵连乘——动态规划
    线段树&树状数组
    SpringAOP
  • 原文地址:https://www.cnblogs.com/fengzheng/p/2858053.html
Copyright © 2011-2022 走看看