修复 PyCharm 使用中文字符 Python 报错的完美解决方案 All In One
1.Python List Comprehensions All In One2.Python 3 List Type errors All In One3.Python Files All In One4.Python exceptions All In One5.Python decorator method and decorator property All In One6.Python data hiding All In One 7.Python Magic Methods & Operator Overloading All In One8.How to fix the for...in loop errors in Python All In One9.How to check function arguments type in Python All In One10.Python rpi_ws281x library All In One11.Python function argument All In One12.How to use variable in Python String All In One13.How to change the default Python2 to Python3 on Linux All In One14.Python & PEP All In One15.How to use pip3 install the latest version package All In One16.Python for loop with index All In One17.Python try...catch All In One18.Python OOP & Class private method All In One19.Python range function All In One20.pip3 & python3 -m pip All In One21.Python 3 alias All In One22.Python Ternary Operator All In One23.The principle of uploading files with command line tools All In One24.Python __init__() method & __init__.py file All In One25.Python relative import local package module file All In One26.Python check whether a list includes some value All In One27. Python timezone package All In One28.Python script get date and time All In One29.如何把一个 Python 项目包发布到 PyPI 上指南教程 All In One30.Python 3 vs Python 2 All In One31.Python 脚本接收命令行参数的多种方式 All In One32.Python 数据类型转换 All In One33.Python errors All In One34.Python 元组解构 All In One35.Python custom modify the __add__ method All In One36.Python 3 function & *args & **kwargs All In One37.Python list methods All In One38.Python 字符串插值 All In One39.Pycharm 如何自定义新建的 Python 文件的注释模版 All in One40.Python & PEP 8 & Style Guide All In One41.小甲鱼 All In One42.PyScript All In One43.QRcode ORC All In One44.Python 3 Data Types All In One45.free Python ebook & videos46.Python API Frameworks All In One47.Python Read JSON File48.2020~2021 职业规划书49.Python Web Framework All In One50.Python errors All In One51.如何使用 Python 编写后端 API 接口52.Python Coding Interview All In One53.Jupyter All In One54.How to use PyPI to publish a Python package All In One55.Python3 & Decorators with arguments & @Decorators with arguments bug56.Python Lambda & Functional Programming57.Python & file operation mode58.Python Turtle59.Python 2 to Python 3 convert60.Spyder & Kite61..pyc & Python62.Python Learning Paths63.Python Web Frameworks64.PEP 8 & Style Guide65.Python Crawler All In One66.Python Quiz & Python Exercise67.Anaconda68.Python module all in one69.PIP & Python packages management All In One70.Versatile Python 3.x71.Python Tutorials72.Flask73.selenium & python74.如何抓取电商的数据 & Python75.Python & dict & switch...case All In One76.Spyder & Python All In One77.How to install python3 on macOS All In One78.NLP & AI79.macOS & Python & Redis All In One80.Python & Spider81.How to run multiple Python versions on Windows?82.MySQL & Python83.Python file 操作 open 官方的文档84.Python关键字查询85.常见算法:python 一个简单的方法来实现Fibonacci序列只用迭代器,没有任何复杂的递归数据结构!86.如何在pycharm中切换python版本(2/3) 的图解教程
87.修复 PyCharm 使用中文字符 Python 报错的完美解决方案 All In One
88.how to updating Node.js and npm89.PEP 8 -- Style Guide for Python Code All In One90.如何在 Python 中使用 UTF-8 编码 All In One91.sphinx 文档生成器 (基于 python )92.使用 ReStructuredText + Sphinx + Python 开发wiki ebooks!93.如何在Eclipse正确安装配置PyDev插件的官方教程,以及error 问题的解决方法:94.在线的代码托管平台 coding.net ===中国扩展版github95.python ( pycharm EDU)修复 PyCharm 使用中文字符 Python 报错的完美解决方案 All In One
#_*_ coding:utf-8_*_
https://www.python.org/dev/peps/pep-0263/
到python 的官网看了一下,找到了问题的根本原因!
-
python 默认使用 ASCII 作为标准编码格式;
-
python 指定字符编码格式的时候,必须使用以下3种方式之一:
(不同系统,不同编辑器,可能不同,都实验一下就能找到了!)
# coding=<encoding name>
或
#!/usr/bin/python
# -*- coding: <encoding name> -*-
或
#!/usr/bin/python
# vim: set fileencoding=<encoding name> :
-
一定要把 指定编码格式的语句放在.py文件的第一/第二行,
因为python 文件的第一/第二行必须要满足这个regular expression "^[ \t\v]*#.*?coding[:=][ \t]*([-_.a-zA-Z0-9]+)"
只要严格遵守以上的3点要求,就可以从根本上解决中文输出是报错的问题了!
第一种:
# coding= utf8
第二种:
#!/user/bin/python
# _*_coding: utf8 _*_
第三种:
#!/user/bin/python
# vim: set filecoding=utf8 :
https://www.python.org/dev/peps/pep-0263/
Defining the Encoding (注意 空格)
Python will default to ASCII as standard encoding if no other
encoding hints are given.
To define a source code encoding, a magic comment must
be placed into the source files either as first or second
line in the file, such as:
<span style="color: #00ff00;"> # coding=<encoding name></span>
or (using formats recognized by popular editors)
<span style="color: #00ff00;">#!/usr/bin/python
# -*- coding: <encoding name> -*-</span>
or
<span style="color: #00ff00;">#!/usr/bin/python
# vim: set fileencoding=<encoding name> :</span>
More precisely, the first or second line must match the regular
expression "^[ \t\v]*#.*?coding[:=][ \t]*([-_.a-zA-Z0-9]+)".
The first group of this
expression is then interpreted as encoding name. If the encoding
is unknown to Python, an error is raised during compilation. There
must not be any Python statement on the line that contains the
encoding declaration. If the first line matches the second line
is ignored.
To aid with platforms such as Windows, which add Unicode BOM marks
to the beginning of Unicode files, the UTF-8 signature
'\xef\xbb\xbf' will be interpreted as 'utf-8' encoding as well
(even if no magic encoding comment is given).
If a source file uses both the UTF-8 BOM mark signature and a
magic encoding comment, the only allowed encoding for the comment
is 'utf-8'. Any other encoding will cause an error.
Examples
These are some examples to clarify the different styles for
defining the source code encoding at the top of a Python source
file:
1. With <strong>interpreter binary</strong> and using <strong>Emacs style</strong> file encoding
comment:
<span style="color: #ff00ff;">#!/usr/bin/python
# -*- coding: latin-1 -*-</span>
import os, sys
...
<span style="color: #ff00ff;">#!/usr/bin/python
# -*- coding: iso-8859-15 -*-</span>
import os, sys
...
<span style="color: #ff00ff;">#!/usr/bin/python
# -*- coding: ascii -*-</span>
import os, sys
...
2. <strong>Without interpreter line</strong>, using plain text:
# This Python file uses the following <span style="background-color: #ffff00;">encoding: utf-8</span>
import os, sys
...
3. Text editors might <strong>have different ways</strong> of defining the file's
encoding, e.g.
#!/usr/<span style="background-color: #ffff00;"><strong>local</strong></span>/bin/python
# coding: latin-1
import os, sys
...
4. <strong>Without <span style="background-color: #ffff00;">encoding</span> comment</strong>, Python's parser will <strong>assume ASCII</strong>
text:
#!/usr/local/bin/python
import os, sys
...
5. Encoding comments which don't work:
<span style="background-color: #ffff00;">Missing "coding:" prefix:</span>
#!/usr/local/bin/python
# latin-1
import os, sys
...
<strong><span style="background-color: #ffff00;">Encoding comment not on line 1 or 2:</span></strong>
#!/usr/local/bin/python
#
<span style="color: #ff0000;"> # -*- coding: latin-1 -*-</span>
import os, sys
...
<span style="background-color: #ffff00;">Unsupported encoding:</span>
#!/usr/local/bin/python
# -*- coding: <strong><span style="color: #ff0000;">utf-42</span></strong> -*-
import os, sys
...
error
__author__ = 'xray'
# coding: utf8
# TempConvert.py
val = input("请输入带温度表示符号的温度值(例如: 32C): ")
if val[-1] in ['C', 'c']:
f = 1.8 * float(val[0:-1]) + 32
print("转换后的温度为: %.2fF" % f)
elif val[-1] in ['F', 'f']:
c = (float(val[0:-1]) - 32) / 1.8
print("转换后的温度为: %.2fC" % c)
else:
print("输入有误")
('\n'
' # elif !== else if\n'
' #_*_ coding:cp936_*_\n'
'## coding: utf8\n'
'## coding: gbk\n'
)
'''
# elif !== else if
#_*_ coding:cp936_*_
## coding: utf8
## coding: gbk
'''

solution
# coding: utf8
# TempConvert.py
val = input("请输入带温度表示符号的温度值(例如: 32C): ")
if val[-1] in ['C','c']:
f = 1.8 * float(val[0:-1]) + 32
print("转换后的温度为: %.2fF"%f)
elif val[-1] in ['F','f']:
c = (float(val[0:-1]) - 32) / 1.8
print("转换后的温度为: %.2fC"%c)
else:
print("输入有误")

refs
©xgqfrms 2012-2021
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/5887132.html
未经授权禁止转载,违者必究!
合集:
Python Script
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)