繁体   English   中英

列表中的表格数据

[英]Tabular data from lists

我对 python 的经验很少,我目前正在尝试根据以下数据构建一个表,该表是从 while 循环构建的。

Fahrenheit = 0
CTempList = [0]
FTempList = []
NameList = ["Temperature in Celsius "," Temperature in Fahrenheit"]
while (Celsius <= 100):
    Fahrenheit = (Celsius * 9/5) + 32
    FTempList.append(Fahrenheit)
    Celsius = Celsius + 5 

我想构建一个看起来像这样的表格,目前我无法使用诸如 Prettytable 或 tabulate 之类的模块。

Temperatures     Temperature
in Celcius       in Fahrenheit
------------------------------
  0        |           32.0
  5        |           41.0
 10        |           50.0
 15        |           59.0
 20        |           68.0
 25        |           77.0
 30        |           86.0
 35        |           95.0
 40        |          104.0
 45        |          113.0
 50        |          122.0
 55        |          131.0
 60        |          140.0
 65        |          149.0
 70        |          158.0
 75        |          167.0
 80        |          176.0
 85        |          185.0
 90        |          194.0
 95        |          203.0
100        |          212.0

您可以调整宽度以使其适合您的需要:

Fahrenheit = 0
CTempList = [0]
FTempList = []
NameList = ["Temperature in Celsius "," Temperature in Fahrenheit"]
Celsius = CTempList[0]
while (Celsius <= 100):
    Fahrenheit = (Celsius * 9/5) + 32
    FTempList.append(Fahrenheit)
    Celsius = Celsius + 5 
    CTempList.append(Celsius)
width = 15
header1 = 'Temperature'.ljust(width,' ')
header2 = 'in Celsius'.ljust(width,' ') + 'in Fahrenheit'.ljust(width,' ')
Header = header1+header1 + '\n' +header2
print(Header)
print('-'*(width+width))
for C, F in zip(CTempList, FTempList):
  line = str(C).rjust(3,' ') + '|'.rjust(10,' ') + str(F).center(width, ' ')
  print(line)

输出:

Temperature    Temperature
in Celsius     in Fahrenheit
------------------------------
  0         |      32.0
  5         |      41.0
 10         |      50.0
 15         |      59.0
 20         |      68.0
 25         |      77.0
 30         |      86.0
 35         |      95.0
 40         |     104.0
 45         |     113.0
 50         |     122.0
 55         |     131.0
 60         |     140.0
 65         |     149.0
 70         |     158.0
 75         |     167.0
 80         |     176.0
 85         |     185.0
 90         |     194.0
 95         |     203.0
100         |     212.0

如果你想打印到终端,可以这样做:

Celsius = 0
CTempList = []
FTempList = []
NameList = ["Temperature in Celsius "," Temperature in Fahrenheit"]
while (Celsius <= 100):
    Fahrenheit = (Celsius * 9/5) + 32
    FTempList.append(Fahrenheit)
    CTempList.append(Celsius)
    Celsius = Celsius + 5 

print(NameList[0]+"   "+NameList[1])
print("___________________")
for i in range(0, len(FTempList)):
    print("  %d     |     %d  " % (CTempList[i], FTempList[i]))

你可能需要让它更好一点。 例如,由于某些数字具有更多字符,因此该行中有一个中断,这看起来不太好。 您可以在此处添加 if 语句以确保格式保持一致。

如果您想写入文件,可以采用以下一种方法:

newfile = open('prettytable.txt','w')
Celsius = 0
CTempList = [0]
FTempList = []
NameList = ["Temperature in Celsius "," Temperature in Fahrenheit"]
print(NameList[0],'\t', NameList[1],'-'*53, file=newfile)
while (Celsius <= 100):
    Fahrenheit = (Celsius * 9/5) + 32
    if len(str(Celsius)) == 1:
      print(f'  {Celsius}', ' '*20, '|', ' '*20, Fahrenheit, file=newfile)
    elif len(str(Celsius)) == 2:
      print(f' {Celsius}', ' '*20, '|', ' '*20, Fahrenheit, file=newfile)
    else:
      print(Celsius, ' '*20, '|', ' '*20, Fahrenheit, file=newfile)
    # FTempList.append(Fahrenheit)
    Celsius = Celsius + 5

newfile.close()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM