关于Python字符串显示u...的解决方式
作者:qijingpei 时间:2021-12-21 18:58:58
版本:python2.7 2.7 2.7!!!
症状:比如,我编写了一个字符串number,输出到网页上,变成了u'number'
解决方法:
num = "number".encode('utf-8')
print(num)
即把它以‘utf-8'编码形式编码,
注意encode('utf-8')方法对list和dict类型应该是不支持,如果你想把这些字符串存到list或者dict中,
我的办法是用上面的发放先转换成‘utf-8'编码,然后再存到list或者dict中
另外:有人说这只是一种编码形式,不影响程序运行,但是我要从网页上获取这些信息并通过json解析,所以我不能让它有u'number'这样的字符串出现
原因:python2.7支持unicode编码和utf-8编码两种,显示时显示成u‘number'表明这是一个unicode编码的字符串,所以转换成utf-8就不显示成u'number'啦!~
补充知识:python u开头转str
我就废话不多说啦,大家还是直接看代码吧!
c = "\\u4f60\\u597d--9" # 字符串类型的里面是Unicode
d = c.encode('utf-8') # 先字符串转bytes
e = d.decode('unicode_escape') # 再bytes转字符串但是以Unicode转
print(type(d))
print(d)
print(e)
输出为:
<class 'bytes'>
b'\\u4f60\\u597d--9'
你好--9
来源:https://blog.csdn.net/qijingpei/article/details/72993580
标签:Python,字符串,u...
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Ubuntu18.04下python版本完美切换的解决方法
2021-08-22 11:24:19
![](https://img.aspxhome.com/file/2023/9/112129_0s.jpg)
python神经网络tf.name_scope和tf.variable_scope函数区别
2021-01-24 13:10:48
URL编码“陷阱”
2008-03-04 16:57:00
MySql循环插入数据
2010-10-14 13:50:00
Python3实现将文件树中所有文件和子目录归档到tar压缩文件的方法
2022-11-29 08:22:38
python实现在线翻译功能
2023-06-02 22:12:38
解决PyCharm同目录下导入模块会报错的问题
2023-06-12 22:39:55
Python版的文曲星猜数字游戏代码
2023-08-12 16:39:19
python贪吃蛇核心功能实现下
2021-08-23 00:32:37
![](https://img.aspxhome.com/file/2023/8/124148_0s.png)
python-numpy-指数分布实例详解
2022-01-14 07:00:32
ajax返回中文乱码问题解决
2009-04-13 16:07:00
Python中shape[0]、shape[1]和shape[-1]分别的意思详解(附代码)
2021-01-23 05:57:20
python中virtualenvwrapper安装与使用
2022-07-28 03:21:52
使用Python实现给企业微信发送消息功能
2021-06-10 15:30:07
![](https://img.aspxhome.com/file/2023/0/115630_0s.png)
将一个图片以二进制值的形式存入Xml文件中
2008-09-04 11:24:00
网页制作前台之javascript
2013-07-23 08:32:59
PHP伪协议基本原理介绍
2023-05-30 00:24:11
![](https://img.aspxhome.com/file/2023/9/55369_0s.png)
Python Web框架Flask下网站开发入门实例
2023-10-01 16:31:50
![](https://img.aspxhome.com/file/2023/6/107676_0s.png)
Python中的内置函数isdigit()
2023-05-16 16:10:31
Python自制一个PDF转PNG图片小工具
2023-07-24 11:40:12
![](https://img.aspxhome.com/file/2023/8/115518_0s.png)