我有一个CSV文件中的数据,它告诉我们一个篮球运动员是否肥胖。我可以使用python中的if语句创建一个对齐表吗?
我需要从这个数据做一个完全对齐的表;我有所有的信息来判断一个球员是否肥胖。但是,我有一个“if”语句,可以输出每个肥胖球员的值,并且我需要将这个打印输出成整齐的,对齐的行。
我:
obese_count = 0
total_count = 0
print (" " * 5, "First Name", " " * 2, "Last Name", " " * 2, "Height"," " * 2,"Weight"," " * 2, "BMI") # header
print ("- " * 20)
for player in players:
if has_data(player):
if is_obese(player):
print (" " * 5, player["firstname"]," " * 5, player["lastname"]," " * 9, player["h_feet"]," " * 9,player["h_inches"]," " * 5, player["weight"])
obese_count += 1
total_count += 1
它返回一个极其潦草表:
First Name Last Name Height Weight BMI
- - - - - - - - - - - - - - - - - - - -
Carlos Boozer 6 9 280
Elton Brand 6 8 275
Glen Davis 6 9 289
Thomas Hamilton 7 2 330
James Lang 6 10 305
Jason Maxiell 6 7 280
Oliver Miller 6 9 280
Craig Smith 6 7 272
Robert Traylor 6 8 284
Jahidi White 6 9 290
我想知道是否有什么办法,我可以整理这件事,让我能有一个整洁和对齐的表或至少对齐的行没有不同的间隔。
[Fancier Output Formatting](https://docs.python.org/2/tutorial/inputoutput.html) – 2014-12-02 22:07:03
如[Format Specification Mini-Language](https://docs.python.org/)所示3/library/string.html#formatspec)文档中,您可以使用'width'来定义每个字段与其中一个“对齐”字符的结合宽度(''''''''=''|'“^” ')让它看起来不错。 – martineau 2014-12-02 22:12:00