基本语法
try :
f=open( "D:/tet.txt","r",encoding="UTF-8")
except:
print("出现bug")
捕获指定异常:
try :
print(nmae)
except NameError as e:#变量未定义错误
print("出现bug")
捕获多个异常:
try :
print(1/0)
except (NameError,ZeroDivisionError):
print("出现bug")
捕获所有异常:
try :
print(1/0)
except Exception as e:
print("出现bug")
else和finally异常用法
try :
f=open( "D:/tet.txt","r",encoding="UTF-8")
except Exception as e:
print("出现bug")
else:
print("No bug")
finally:
f.close()
异常的传递
模块的导入
导入全部功能
改名字
包就是一个文件夹,包含一个_init_.py文件是,可包含多个模块文件。
# import my_package.my_module2
# import my_package.my_module1
# import导入
# my_package.my_module2.kun()
# my_package.my_module1.cxk()
# #from导入
# from my_package import my_module1
# from my_package import my_module2
#
# my_module2.kun()
# my_module1.cxk()
#import * 全导入
from my_package import *
# my_module2.kun()#__all__变量控制
my_module1.cxk()
python的字典或者列表嵌套的字典
import json
data=[{"name":"kun","age":16},{"name":"范丞丞","age":17}]
json_str= json.dumps(data,ensure_ascii=False)
print(json_str)
# data= json.loads(data)
s='[{"name": "坤", "age": 16}, {"name": "范丞丞", "age": 17}]'
print(s)
py_s=json.loads(s)
print(py_s)
基础折线图
from pyecharts.charts import Line
from pyecharts.options import TitleOpts,ToolboxOpts,LegendOpts,VisualMapOpts
line = Line()
line.add_xaxis(["蔡徐坤","夏胖子","代雨","巴子"])
line.add_yaxis("虾头指数",[99,88,77,66])
line.set_global_opts(
title_opts=TitleOpts("虾头展示",pos_right="center",pos_bottom="1%"),
legend_opts=LegendOpts(is_show=True),
toolbox_opts=ToolboxOpts(is_show=True),
visualmap_opts=VisualMapOpts(is_show=True),
)
line.render()