python 脚本中 # -*- coding: utf-8 -*-的作用
001、
# -*- coding: utf-8 -*-的作用是告诉python编码,默认的是ASCII编码,使用中文注释的情况下会报错,而增加# -*- coding: utf-8 -*-则程序可以正常执行
[root@pc1 test1]# ls ## 下面是两个测试程序 test01.py test02.py [root@pc1 test1]# cat test01.py ## test01.py程序不包括 # -*- coding: utf-8 -*-, 同时给与中文注释,观察程序运行效果 #!/usr/bin/env python # 注释内容 list1 = ["aa", "bb", "cc"] print(list1) [root@pc1 test1]# cat test02.py ## test02.py程序包括 # -*- coding: utf-8 -**, 同时给与中文注释,观察程序运行效果 #!/usr/bin/env python # -*- coding: utf-8 -*- # 注释内容 list1 = ["aa", "bb", "cc"] print(list1) [root@pc1 test1]# python test01.py ## test01.py程序报错 File "test01.py", line 3 SyntaxError: Non-ASCII character '\xe6' in file test01.py on line 3, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details [root@pc1 test1]# python test02.py ## test02.py程序运算正常 ['aa', 'bb', 'cc']
。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
2023-03-08 samtools 统计重测序数据深度 depth、depth -a、depth -aa的联系与区别
2021-03-08 python中如何获取函数文档
2021-03-08 python中写一个求阶乘的函数
2021-03-08 python中写一个求阶乘的函数
2021-03-08 python中map()内置函数
2021-03-08 python中filter关键字
2021-03-08 python中lambda关键字创建匿名函数