PyCharm 中文 字符 python 报错 的 完美 解决方案!

#_*_ coding:utf-8_*_

https://www.python.org/dev/peps/pep-0263/

到python 的官网看了一下,找到了问题的根本原因!

  1. python 默认使用 ASCII 作为标准编码格式;

  2. python 指定字符编码格式的时候,必须使用以下3种方式之一:

    (不同系统,不同编辑器,可能不同,都实验一下就能找到了!)

    # coding=<encoding name>

    #!/usr/bin/python

    # -*- coding: <encoding name> -*-

    #!/usr/bin/python

    # vim: set fileencoding=<encoding name> :

  3. 一定要把 指定编码格式的语句放在.py文件的第一/第二行

    因为python 文件的第一/第二行必须要满足这个regular expression "^[ \t\v]*#.*?coding[:=][ \t]*([-_.a-zA-Z0-9]+)"

只要严格遵守以上的3点要求,就可以从根本上解决中文输出是报错的问题了!

demo:

第一种:
# 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: # coding=<encoding name> or (using formats recognized by popular editors) #!/usr/bin/python
# -*- coding: <encoding name> -*- or #!/usr/bin/python
# vim: set fileencoding=<encoding name> : 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 interpreter binary and using Emacs style file encoding
comment: #!/usr/bin/python
# -*- coding: latin-1 -*-
import os, sys
... #!/usr/bin/python
# -*- coding: iso-8859-15 -*-
import os, sys
... #!/usr/bin/python
# -*- coding: ascii -*-
import os, sys
... 2. Without interpreter line, using plain text: # This Python file uses the following encoding: utf-8
import os, sys
... 3. Text editors might have different ways of defining the file's
encoding, e.g. #!/usr/local/bin/python
# coding: latin-1
import os, sys
... 4. Without encoding comment, Python's parser will assume ASCII
text: #!/usr/local/bin/python
import os, sys
... 5. Encoding comments which don't work: Missing "coding:" prefix: #!/usr/local/bin/python
# latin-1
import os, sys
... Encoding comment not on line 1 or 2: #!/usr/local/bin/python
#
# -*- coding: latin-1 -*-
import os, sys
... Unsupported encoding: #!/usr/local/bin/python
# -*- coding: utf-42 -*-
import os, sys
...

1

__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
'''

demo:

# 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("输入有误")

1

1

1

1

1

1

1

1

1

1

1

1

1

1

 

PyCharm 中文 字符 python 报错 的 完美 解决方案!的更多相关文章

  1. 关于Entity Framework中的Attached报错的完美解决方案终极版

    之前发表过一篇文章题为<关于Entity Framework中的Attached报错的完美解决方案>,那篇文章确实能解决单个实体在进行更新.删除时Attached的报错,注意我这里说的单个 ...

  2. 关于Entity Framework中的Attached报错的完美解决方案

    我们在使用Entity Framework进行CRUD时,为了提升查询效率,一般均会启动NoTracking,即不追踪变化,设置代码如下: //这是DB First模式下设置方法: aTestEnti ...

  3. pycharm 中 import requests 报错

    一 , 使用Pycharm来抓取网页的时候,要导入requests模块,但是在pycharm中 import requests 报错. 原因: python中还没有安装requests库 解决办法: ...

  4. PyCharm导入tensorflow包报错的问题

    [注]PyCharm导入tensorflow包报错的问题 若是你也遇到这个问题,说明你也没有理解tensorflow到底在哪里. 当安装了anaconda3.6后,在PyCharm中设置interpr ...

  5. Python报错module 'scipy.misc' has no attribute 'xxx'

    Python报错module 'scipy.misc' has no attribute 'imresize' 解决办法: 安装Pillow包,命令如下: pip install Pillow 然后重 ...

  6. 【adb命令】在cmd窗口中使用adb install命令安装 中文名字apk报错的解决办法

    1.在cmd窗口中使用adb install命令安装中文名字apk报错,安装英文名字apk就正常,详细报错如下图: 2.查看adb版本号:adb version 3.怀疑是adb版本的原因,尝试换个最 ...

  7. weblogic 包里面有中文文件名 会报错

    目前:没有解决,只要有中文启动就报错 http://bbs.csdn.net/topics/10055670 http://www.2cto.com/os/201406/311394.html

  8. Eclipse中引入com.sun.image.codec.jpeg包报错的完美解决办法

    转: Eclipse中引入com.sun.image.codec.jpeg包报错的完美解决办法  更新时间:2018年02月14日 17:13:03   投稿:wdc   我要评论   Java开发中 ...

  9. Python报错总结丶自定义报错

    Python报错总结: 常见异常 1,NameError: name 'a' is not defined:未定义函数名             2,IndentationError: uninden ...

随机推荐

  1. 【UNIAPP】接入导航系统完整版

    // 查询附近/搜索关键词 <template> <view> <!--地图容器--> <map id="myMap" :markers= ...

  2. 详解SpringMVC

    详解SpringMVC 一.什么是MVC? ​ MVC是模型(Model).视图(View).控制器(Controller)的简写,是一种软件设计规范.就是将业务逻辑.数据.显示分离的方法来组织代码. ...

  3. CSS居中的常用方式以及优缺点

    前言 居中是页面开发中经常遇到的问题. 使用合适的.简单的.兼容性好的居中方式是我们页面仔在整个工作生涯中都要面对的问题. text-align:center 来看这个例子,一张图片和文字进行居中.如 ...

  4. jsaper子报表Subreport(父子报表互相传值)

    有很多人都说Jasperreports不适合中国式复杂报表,实际上运用好父子报表可以解决大部分问题了.例如下面的表.每个学生的学科数目不固定,且每个学生后有相当于小计的平均分.有点复杂度的报表,可以使 ...

  5. oss-server 小型对象存储系统

    oss-server 项目介绍 oss-server是针对项目开发时提供的小型对象存储系统,开发者在针对文件上传时业务剥离,同时方便文件迁移,为满足单个项目,多个系统的情况下,提供统一的oss服务 o ...

  6. Think in Java 第三章操作符

    Think in Java 第三章操作符 赋值 对象赋值 ​ 我们真正操作的是对对象的引用.所以倘若"将一个对象赋值给另一个对象",实际上是将"引用"从一个地方 ...

  7. Java帝国的成立

    java帝国的成立 一场旷日持久的战争 1972年C语言诞生 贴近硬件 ,运行极快 , 效率极低 操作系统, 编译器 ,数据库, 网络系统 指针和内存 (容易犯错 , 暴力) 1982 年C++诞生 ...

  8. 6127:Largest Average

    #include<bits/stdc++.h> using namespace std; int a[100001]; double ave[100001]; struct student ...

  9. httprunner(3)用脚手架快速搭建项目

    前言 如何快速搭建一个httprunner项目呢?我们可以使用脚手架,脚手架就是自动地创建一些目录,形成一个项目的架构,不需要我们再手动的去创建 查看创建新项目的命令 先来查看一下帮助命令 httpr ...

  10. docker(10)上传本地镜像到镜像仓库

    前言 之前通过docker搭建过jenkins+python3环境,如果想要在不同的机器上搭建一样的环境,就可以将之前搭建的镜像上传到镜像仓库,这样方便在不同的机器上快速搭建同一套环境. 如果公开的话 ...