python中的str()与eval函数

摘要:
author:headsenchendate:2018-04-0910:48:22eval函数是把str转化成list、dict、tuplestr函数把list,dict,tuple转为为字符串----------------------------------------------------------------#字符串转换成列表a="[[1,2],[3,4],[5,6],[7,8],[9,0]]"printb=evalprint-------------------------------------------------------------#字符串转换成字典a="{1:'a',2:'b'}"printb=evalprint{1:'a',2:'b'}----------------------------------------------------------#字符串转换成元组a=""printb=evalprint

author:headsen chen

date:2018-04-09 10:48:22

eval函数是把str转化成list、dict、tuple

str函数把list,dict,tuple转为为字符串

----------------------------------------------------------------
# 字符串转换成列表
a = "[[1,2], [3,4], [5,6], [7,8], [9,0]]"
print(type(a))
b = eval(a)
print(b,type(b))

<class 'str'>
([1, 2], [3, 4], [5, 6], [7, 8], [9, 0]) <class 'tuple'>

-------------------------------------------------------------

# 字符串转换成字典

a = "{1: 'a', 2: 'b'}"
print(type(a))
b = eval(a)
print(b,type(b))

<class 'str'>
{1: 'a', 2: 'b'} <class 'dict'>

----------------------------------------------------------

# 字符串转换成元组
a = "([1,2], [3,4], [5,6], [7,8], (9,0))"
print(type(a))
b=eval(a)
print(b,type(b))

<class 'str'>
([1, 2], [3, 4], [5, 6], [7, 8], [9, 0]) <class 'tuple'>

免责声明:文章转载自《python中的str()与eval函数》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Python Linux系统管理之文件与文件路径管理操作系统——进程,线程,锁下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

python之路——表操作

阅读目录   引擎介绍   表介绍   创建表   查看表结构   mysql中的数据类型   表的完整性约束   修改表结构   删除表   多表结构的创建与分析   作业 返回顶部 引擎介绍 mysql中的存储引擎(https://www.cnblogs.com/l-hf/p/11533999.html) 返回顶部 表介绍 表就相当于文...

文本检测和识别 代码结构梳理

前言:最近学习了一些OCR相关的基础知识,包含目标检测和自然语言处理。 正好,在数字中国有相关的比赛: https://www.datafountain.cn/competitions/334/details/rule 所以想动手实践一下,实际中发现,对于数据标签的处理和整个检测和识别的流程并不熟悉,自己从头去搞还是有很大难度。 幸好,有大佬们之前开源的一...

postman 断言学习

请求 url :https://www.v2ex.com/api/nodes/show.json?name=python get请求 postman发起请求并做断言 断言: tests["Body matches string"] = responseBody.has("这里讨论各种 Python 语言编程话题,也包括 Django,Tornado 等框...

shell实现trim函数-去除字符串两侧的空格(包括tab,space键)

shell实现trim函数效果去除字符串两侧的空格,以下三个命令等价,都能实现 sed 's/^s*//' totrim.txt |sed 's/s*$//'>trimed.txtsed 's/^s*//;s/s*$//' totrim.txt>trimed.txtsed -e 's/^s*//' -e 's/s*$//' totrim.txt...

进程与线程(2)- python实现多进程

python实现多进程 参考链接:https://morvanzhou.github.io/tutorials/python-basic/multiprocessing/ python中实现多进程的模块:multiprocessing 注意:在windows系统下,要想启动一个子进程,必须把进程相关的内容写在”if __name__ == “__main...

【python】threadpool的内存占用问题

先说结论: 在使用多线程时,不要使用threadpool,应该使用threading, 尤其是数据量大的情况。因为threadpool会导致严重的内存占用问题! 对比threading和threadpool的内存占用 # coding=utf-8 import time import os import psutil import json...