将python字符串转化成长表达式的函数eval实例
作者:CY_TEC 时间:2022-08-25 18:27:23
爬一个网页时,要保存的数据都没有encode,就导致保存下来的中文都变成unicode了。。。
那么,怎么把一个表示字符串的unicode还原成unicode呢?
函数eval(expression)就可以帮助完成这项工作。
例如:
>>>> a = "u'\\u674e'"
>>> print a
u'\u674e'
>>> print eval(a)
李
该函数的描述:
The expression argument is parsed and evaluated as a Python expression
也就是说这个函数可以把expression重新转化成python表达式。
附带的例子如下:
>>>> x = 1
>>> print eval('x+1')
2
补充知识:python基础 字符串 用str函数或repr函数将特殊字码转换为字字符串
为避免错误 有时在编程内会遇到特殊字符 可调用str函数或者repr函数将非字符串表示为字符串
例如:
>>>xxx = 2012
>>>print('hello '+'world '+xxx)
错误
#但如果利用str函数或repr函数
>>>xxx = 2012
>>>print('hello '+'world '+str(xxx))
>>>print('hello '+'world '+repr(xxx))
>>>hello world 2012
来源:https://blog.csdn.net/CY_TEC/article/details/51011020
标签:python,字符串,表达式,eval
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
tensorflow入门:tfrecord 和tf.data.TFRecordDataset的使用
2022-06-29 16:23:40
![](https://img.aspxhome.com/file/2023/3/86553_0s.jpg)
Server 对象 错误 ASP 0177 800401f3 的解决方案 Server 对象 错误 ASP 0177 800401f3
2009-07-28 17:57:00
Pytorch中的Tensorboard与Transforms搭配使用
2023-07-09 08:27:05
![](https://img.aspxhome.com/file/2023/2/62022_0s.png)
解读iPhone平台的一些优秀设计思路
2010-06-24 21:53:00
![](https://img.aspxhome.com/file/UploadPic/20106/24/iphone_design_banner-88s.jpg)
Golang中 Slice的分析与使用源码解析
2023-06-19 03:48:13
![](https://img.aspxhome.com/file/2023/4/87614_0s.jpg)
python实现简单ftp客户端的方法
2023-12-14 20:07:31
完美解决phpstudy安装后mysql无法启动(无需删除原数据库,无需更改任何配置,无需更改端口)直接共存
2023-11-14 17:39:47
![](https://img.aspxhome.com/file/2023/0/95620_0s.jpg)
对python模块中多个类的用法详解
2021-03-17 17:27:21
Python文件打开读取写入方法实用案例
2023-08-23 21:19:51
![](https://img.aspxhome.com/file/2023/2/61982_0s.png)
详解pandas获取Dataframe元素值的几种方法
2022-12-28 07:30:01
防注入asp过滤sql特殊字符函数
2007-10-23 17:50:00
优化代码 改善CSS文件可读性
2008-06-13 13:50:00
![](https://img.aspxhome.com/file/UploadPic/20086/13/2008613141551898s.gif)
uniapp路由uni-simple-router实例详解
2023-09-24 21:27:04
python实现双色球随机选号
2021-06-26 05:11:09
css基础教程之序曲
2008-07-23 12:40:00
asp如何在ADO中客户端利用好缓存技术?
2010-06-17 12:50:00
正则 global 属性介绍
2008-03-18 20:50:00
Python中扩展包的安装方法详解
2021-09-19 23:35:46
![](https://img.aspxhome.com/file/2023/8/83848_0s.png)
运行SQL Server的计算机间移动数据库
2009-01-20 13:07:00
Python中的pprint模块
2022-02-07 17:02:42