Python中文编码知识点

yipeiwu_com6年前Python基础

如何用 Python 输出 "Hello, World!",英文没有问题,但是如果你输出中文字符"你好,世界"就有可能会碰到中文编码问题。
Python 文件中如果未指定编码,在执行过程会出现报错:

#!/usr/bin/python
print "你好,世界";

以上程序执行输出结果为:

File "test.py", line 2
SyntaxError: Non-ASCII character '\xe4' in file test.py on line 2, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details

Python中默认的编码格式是 ASCII 格式,在没修改编码格式时无法正确打印汉字,所以在读取中文时会报错。
解决方法为只要在文件开头加入 # -*- coding: UTF-8 -*- 或者 #coding=utf-8 就行了

注意:#coding=utf-8 的 = 号两边不要空格。

#!/usr/bin/python
# -*- coding: UTF-8 -*-
 
print "你好,世界";

输出结果为:

所以如果大家在学习过程中,代码中包含中文,就需要在头部指定编码。

注意:Python3.X 源码文件默认使用utf-8编码,所以可以正常解析中文,无需指定 UTF-8 编码。
注意:如果你使用编辑器,同时需要设置 py 文件存储的格式为 UTF-8,否则会出现类似以下错误信息:

SyntaxError: (unicode error) ‘utf-8' codec can't decode byte 0xc4 in position 0:
invalid continuation byte

Pycharm 设置步骤:

  • 进入 file > Settings,在输入框搜索 encoding。
  • 找到 Editor > File encodings,将 IDE Encoding 和 Project Encoding 设置为utf-8。

相关文章

Python-嵌套列表list的全面解析

一个3层嵌套列表m m=["a",["b","c",["inner"]]] 需要解析为基本的数据项a,b,c,inner 基本的取数据项方法: for i in m: print i这...

python进行两个表格对比的方法

如下所示: # -*- coding:utf-8 -*- import xlrd import sys import re import json dict1={} dict2={...

Django ORM 练习题及答案

1.modles中表结构 #出版社 class Publisher(models.Model): name = models.CharField(max_length=32)...

python异步实现定时任务和周期任务的方法

一. 如何调用 def f1(arg1, arg2): print('f1', arg1, arg2) def f2(arg1): print('f2', arg1)...

Pytorch 搭建分类回归神经网络并用GPU进行加速的例子

Pytorch 搭建分类回归神经网络并用GPU进行加速的例子

分类网络 import torch import torch.nn.functional as F from torch.autograd import Variable impor...