Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
228 views
in Technique[技术] by (71.8m points)

Prettytable库输出中文的问题

 1 #!/usr/bin/env python
 2 # -*- coding:utf-8 -*-
 3 #Author: ####
 4 
 5 from prettytable import PrettyTable
 6 x = PrettyTable(["姓名", "性别", "年龄", "存款"])
 7 
 8 x.align["姓名"] = "1" #以姓名字段左对齐
 9 x.padding_width = 1  # 填充宽度
10 x.add_row(["赵一","男", 20, 100000])
11 x.add_row(["钱二","男", 21, 500])
12 x.add_row(["孙三", "男", 22, 400.7])
13 x.add_row(["李四", "男", 23, 619.5])
14 x.add_row(["周五", "男", 24, 1214.8])
15 x.add_row(["吴六", "女", 25, 646.9])
16 x.add_row(["郑七", "女", 26, 869.4])
17 x.add_row(["王七加一", "男", 21, 869.4])
18 
19 print(x)

网上有这样一个示例,能够格式化输出中文标题并且不乱码,但是我运行这段代码之后输出的是乱码标题
是因为python版本不对的问题吗?python2.7环境下要怎么设置才能输出中文不乱码?

ps:在jupyter notebook里输出是正常的,在命令行环境下直接运行输出就乱码
图片描述
python环境如下
图片描述


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

我在pycharm下输出是正常的, 你在中文前面加个u试试, 如: x.add_row([u"赵一","男", 20, 100000])


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...