


The example in this article describes the problem of garbled characters generated when executing Python files under the windows command window. For your reference:
Ps: Please correct me if there are any mistakes. Welcome to exchange and learn
# -*- coding:utf-8 -*-str = "彦雪"print str
After execution, the output result is as follows:
褰﹂洩
The garbled code results may be different from everyone else, but they are all garbled codes!!
Problem analysis
Python2 default encoding is "ascii", ascii encoding does not include Chinese characters
If there are Chinese characters in it, the Python interpreter will generally report an error.
But if UTF-8 encoding is specified, Python will no longer Error report.
"# -- coding:utf-8 --" specifies that the Python source code is encoded in UTF-8.
The default encoding of window is gbk encoding, so str must be encoded as gbk before output.
Since Python does not allow you to directly convert utf-8 to gbk, you need to convert utf-8 to unicode first and then to gbk
In-depth analysis
This method has one One disadvantage is that problems will arise when we are cross-platform, so Python provides us with a convenient solution to use unicode as output--this method does not apply to raw_input
when printing is required When outputting, Python will first call the encoding format of the character output program (command line or output function), and then encode the string into the encoding used by the character output program (so that the character output program will not appear because it does not recognize the encoding) Garbled characters), and then the character output program outputs the encoded characters to the destination.
Solution
# 方法一 中文前加u, 告诉Python解释器后面的是个unicode编码str = u"彦雪"
# 方法二 str.decode('utf-8') 以utf-8编码对字符串 str 进行解码, 获取unicodestr = "彦雪".decode('utf-8')
# 方法三 unicode(str, 'utf-8') 将字符串 str 以utf-8编码解码, 获取unicodestr = unicode('彦雪','utf-8')
raw_input displays garbled characters in the windows command window
Using raw_input requires converting Chinese to system encoding. The method is as follows
# 方法1 str.encode("gbk") 将unicode转为gbk 编码content = raw_input(u"输入内容: ".encode("gbk"))
# 方法2content = raw_input("输入内容: ".decode('utf-8').encode("gbk"))
# 方法3content = raw_input(unicode('输入内容: ','utf-8').encode("gbk"))
Although this way of writing It is very convenient, but the cross-platform effect is poor. I personally do not recommend this writing method. It is recommended to write Chinese characters and raw_input separately. Use other means to achieve the purpose of being on the same line
Extended reading
Python's coding notes# -- coding:utf-8 --
PEP 263 -- Defining Python Source Code Encodings
About Python's encoding, garbled characters and Unicode Some research
raw_input input, file reading, variable comparison and other str, unicode, utf-8 conversion issues
Exploration
Currently resigned, while waiting, Let me share with you the problems I often encountered before and discuss them with you. I hope it will be helpful to everyone. Corrections are welcome
Find yourself in sharing knowledge and enjoy the joy of programming
The above is the detailed content of Python2.7 outputs Chinese garbled characters in the windows command window. For more information, please follow other related articles on the PHP Chinese website!

c盘的users是用户文件夹,主要存放用户的各项配置文件。users文件夹是windows系统的重要文件夹,不能随意删除;它保存了很多用户信息,一旦删除会造成数据丢失,严重的话会导致系统无法启动。

启动任务管理器的三个快捷键是:1、“Ctrl+Shift+Esc”,可直接打开任务管理器;2、“Ctrl+Alt+Delete”,会进入“安全选项”的锁定界面,选择“任务管理器”,即可以打开任务管理器;3、“Win+R”,会打开“运行”窗口,输入“taskmgr”命令,点击“确定”即可调出任务管理器。

PIN码是Windows系统为了方便用户本地登录而独立于window账户密码的快捷登录密码,是Windows系统新添加的一套本地密码策略;在用户登陆了Microsoft账户后就可以设置PIN来代替账户密码,不仅提高安全性,而且也可以让很多和账户相关的操作变得更加方便。PIN码只能通过本机登录,无法远程使用,所以不用担心PIN码被盗。

对于刚刚开始使用PHP的用户来说,如果在Windows操作系统中遇到了“php不是内部或外部命令”的问题,可能会感到困惑。这个错误通常是由于系统无法识别PHP的路径导致的。在本文中,我将为您提供一些可能会导致这个问题的原因和解决方法,以帮助您快速解决这个问题。

windows操作系统的特点包括:1、图形界面;直观高效的面向对象的图形用户界面,易学易用。2、多任务;允许用户同时运行多个应用程序,或在一个程序中同时做几件事情。3、即插即用。4、出色的多媒体功能。5、对内存的自动化管理。

win10自带的onenote是UWP版本;onenote是一套用于自由形式的信息获取以及多用户协作工具,而UWP版本是“Universal Windows Platform”的简称,表示windows通用应用平台,不是为特定的终端设计的,而是针对使用windows系统的各种平台。

因为win10系统是不自带扫雷游戏的,需要用户自行手动安装。安装步骤:1、点击打开“开始菜单”;2、在打开的菜单中,找到“Microsoft Store”应用商店,并点击进入;3、在应用商店主页的搜索框中,搜索“minesweeper”;4、在搜索结果中,点击选择需要下载的“扫雷”游戏;5、点击“获取”按钮,等待获取完毕后自动完成安装游戏即可。

在windows中鼠标指针呈四箭头时一般表示选中对象可以上、下、左、右移动。在Windows中鼠标指针首次用不同的指针来表示不同的状态,如系统忙、移动中、拖放中;在Windows中使用的鼠标指针文件还被称为“光标文件”或“动态光标文件”。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Dreamweaver Mac version
Visual web development tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool