下载安卓APP箭头
箭头给我发消息

客服QQ:3315713922

如何将uPYTHON--EVAL()函数转换

作者:课课家教育     来源: http://www.kokojia.com点击数:1105发布时间: 2019-08-09 09:27:01

标签: python教程python数据库操作python培训

大神带你学编程,欢迎选课

eval()函数将公式字符串转换成公式。在数据库应用的过程中,经常要将不同数据类型的数据进行相应转换,满足实际应用的需要。Visual FoxPro系统提供了若干个转换函数,较好地解决了数据类型转换的问题。

如何将uPython--EVAL()函数转换

 我们在平常学习使用Python的时候经常会自己编写一些小程序来使用,虽然python是跨平台的语言,但是如果我们想要在一个没有python以及很多库环境的电脑上使用我们的小程序该怎么办呢?
a = eval("1+2")
print(a,type(a))  # 3 <class 'int'>

b = eval("True")
print(b,type(b))  # True <class 'bool'>

c = eval("1==2")
print(c,type(c))  # False <class 'bool'>

d = eval("true")
print(d)  # NameError: name 'true' is not defined
 

eval()函数实现list、dict、tuple与str之间的转化

  • 字符串转换成列表
a = "[1,2,3]"
print(a,type(a))  # [1,2,3] <class 'str'>

b = eval(a)
print(b,type(b))  # [1, 2, 3] <class 'list'>
  • 字符串转换成元组
a = "(1,2,3)"
print(a,type(a))  # (1,2,3) <class 'str'>

b = eval(a)
print(b,type(b))  # (1, 2, 3) <class 'tuple'>
  • 字符串转换成字典
a = "{1:'a',2:2,3:[1,2,3]}"
print(a,type(a))  # {1:'a',2:2,3:[1,2,3]} <class 'str'>

b = eval(a)
print(b,type(b))  # {1: 'a', 2: 2, 3: [1, 2, 3]} <class 'dict'>

赞(0)
踩(0)
分享到:
华为认证网络工程师 HCIE直播课视频教程