Python中一般处理中文的几种方法
作者:杰瑞26 时间:2023-10-11 01:45:23
Python中的中文是个很头痛的问题,Python2和Python3都会出现,而且py2中出现的概率要大些。
有一道面试题: Python中如何处理中文问题,能想到的就是以下几方面来规避:
1. 首行添加 # coding = utf-8
# coding = utf-8
# 或者
# -*- coding:utf-8 -*-
2. 字符串前添加u
>>> s = u'中文'
>>> print(s)
中文
3. 添加以下代码
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
4. 使用编解码
s.decode('utf-8').encode('gbk')
5. 打开含中文的文件时
open(FileName, 'r', encoding='utf-8')
6. IDLE中设置,比如Pycharm中,将文件编码,保存的形式都设置成一样的,gbk或者utf-8形式的。
来源:https://blog.csdn.net/Jerry_1126/article/details/85269161
标签:python,处理中文
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python组合数据类型详解
2022-10-03 02:23:47
![](https://img.aspxhome.com/file/2023/6/131716_0s.png)
js中arguments的用法(实例讲解)
2024-06-15 08:20:08
Yii2结合Workerman的websocket示例详解
2023-11-17 04:48:40
Laravel 4 初级教程之视图、命名空间、路由
2024-06-05 09:23:26
![](https://img.aspxhome.com/file/2023/7/122857_0s.jpg)
python写日志文件操作类与应用示例
2021-09-29 01:16:38
python模块shutil函数应用示例详解教程
2022-09-29 02:07:36
![](https://img.aspxhome.com/file/2023/2/110242_0s.png)
碰到MySQL无法启动1067错误问题解决方法
2024-01-18 09:16:47
![](https://img.aspxhome.com/file/2023/2/105692_0s.jpg)
python机器学习算法与数据降维分析详解
2023-09-20 19:40:27
![](https://img.aspxhome.com/file/2023/8/67238_0s.png)
python使用pil进行图像处理(等比例压缩、裁剪)实例代码
2022-12-22 16:50:09
原生JS实现匀速图片轮播动画
2024-06-07 15:28:15
![](https://img.aspxhome.com/file/2023/9/122589_0s.jpg)
获取 Textarea 的光标位置
2010-11-30 21:33:00
Python requests上传文件实现步骤
2023-09-25 15:14:50
Django处理文件上传File Uploads的实例
2023-07-18 13:44:23
将Python中的数据存储到系统本地的简单方法
2021-08-22 18:15:55
Mysql锁内部实现机制之C源码解析
2024-01-12 21:13:25
![](https://img.aspxhome.com/file/2023/4/70454_0s.png)
xml xpath基础语法
2008-01-21 12:46:00
Python常用扩展插件使用教程解析
2021-12-22 03:00:47
![](https://img.aspxhome.com/file/2023/0/113130_0s.png)
用Python实现石头剪刀布游戏
2023-06-05 05:37:41
![](https://img.aspxhome.com/file/2023/5/95765_0s.png)
浅谈Python中的闭包
2022-03-08 06:18:31
python中split()函数的用法详解
2022-01-13 14:52:06
![](https://img.aspxhome.com/file/2023/0/134660_0s.png)