utf-8 - python在windows命令行下生成文件乱码问题,如何解决?
阿神
阿神 2017-04-18 10:20:29
0
5
722

编辑一个文件test.py,编码默认是utf-8,代码如下:

print("你好")

命令行下输入:python test.py,显示结果为:你好。
但是当我输入命令:python test.py > test2.py 把输出结果保存到文件,使用编辑器打开test2.py文件是乱码:

在命令行输入type test2.py显示是正常的:

大概直达问题是出在cmd的编码为gbk上,但该怎么解决这个问题?使用:chcp 65001,好像不能解决问题。
谢谢!

阿神
阿神

闭关修行中......

reply all(5)
左手右手慢动作

Use git-bash to search and download one

PHPzhong

CMD defaults to gbk, so if the output string is normal, then the output should be GBK characters. First modify the file encoding, and indicate the encoding of the file in the header of the python file, and then write the file and it will be normal

大家讲道理

Modify test.py like this,请把编码改为gbk:

# -*- coding: gbk -*- 
print('你好')
阿神

The garbled characters are only related to the encoding of your text editor. You are using Atom. In the lower right corner there is UTF-8 , click and select Chinese (GBK)

PHPzhong

Modify cmd default file encoding

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template