我正在尝试用值格式化表格,但觉得我做得不好。例如,在分割和传递到字符串时必须将整数字典转换为整数。而且 %f 似乎也打印了 0 的负载。我只想要一个带小数点的除法,而不是所有尾随的 0
band1= {'channel1': 10564, 'channel2': 10589, 'channel3': 10612, 'channel4': 10637,'channel5': 10662, 'channel6': 10687,
'channel7': 10712, 'channel8': 10737, 'channel9': 10762, 'channel10': 10787,'channel11': 10812, 'channel12': 10837, }
print '%10s %10s %10s %10s %10s %10s %10s %10s %10s %10s %10s %10s' % ("Channel 1", "Channel 2", "Channel 3",
"Channel 4", "Channel 5", "Channel 6", "Channel 7","Channel 8", "Channel 9", "Channel 10", "Channel 11", "Channel 12")
print '%10s %10s %10s %10s %10s %10s %10s %10s %10s %10s %10s %10s' % (band1['channel1'], band1['channel2'], band1['channel3'],
band1['channel4'], band1['channel5'], band1['channel6'], band1['channel7'], band1['channel8'], band1['channel9'],
band1['channel10'], band1['channel11'], band1['channel12'])
print '%5f %5f %5f %5f %5f %5f %5f %5f %5f %5f %5f %5f' % (int(band1['channel1'])/5.0, int(band1['channel2'])/5.0, int(band1['channel3'])/5.0,
int(band1['channel4'])/5.0, int(band1['channel5'])/5.0, int(band1['channel6'])/5.0, int(band1['channel7'])/5.0, int(band1['channel8'])/5.0, int(band1['channel9'])/5.0,
int(band1['channel10'])/5.0, int(band1['channel11'])/5.0, int(band1['channel12'])/5.0)
这给出了:
Channel 1 Channel 2 Channel 3 Channel 4 Channel 5 Channel 6 Channel 7 Channel 8 Channel 9 Channel 10 Channel 11 Channel 12
10564 10589 10612 10637 10662 10687 10712 10737 10762 10787 10812 10837
2112.800000 2117.800000 2122.400000 2127.400000 2132.400000 2137.400000 2142.400000 2147.400000 2152.400000 2157.400000 2162.400000 2167.400000