Python:定长字符串多变量left/right对齐
Python: Fixed length string multiple variables left/right aligned
我是 Python 的新手,我尝试格式化一个字符串以在 LCD 显示器上输出。
我想输出火车出发的格式化table
- 显示屏固定长度为 20 个字符 (20x4)
- 我有 3 个可变长度的字符串变量(线路、站点、eta)
- 其中 2 个应左对齐(线路、车站),而第三个应右对齐
示例:
8: station A 8
45: long station 10
1: great station 25
我玩过各种东西,但我无法定义整个字符串的最大长度,但只能定义 1 个变量:
print('{0}: {1} {2:<20}'.format(line, station, eta))
非常感谢任何提示和提示!
--- 解决方案基于@Rafael Cardoso 的回答:
print(format_departure(line, station, eta))
def format_departure(line, station, eta):
max_length = 20
truncate_chars = '..'
# add a leading space to the eta - just to be on the safe side
eta = ' ' + eta
output = '{0}: {1}'.format(line, station) # aligns left
# make sure that the first part is not too long, otherwise truncate
if (len(output + eta)) > max_length:
# shorten for truncate_chars + eta + space
output = output[0:max_length - len(truncate_chars + eta)] + truncate_chars
output = output + ' '*(max_length - len(output) - len(eta)) + eta # aligns right
return output
您可以使用切片来指定最大长度。例如。以下将仅打印格式产生的字符串的前 20 个字符:
print('{0}: {1} {2:<20}'.format(line, station, eta)[:20])
您可以通过计算在 station 和 eta 之间应添加多少个空格来添加空格:
>>> line = ['8', '45', '1']
>>> station = ['station A', 'long station', 'great station']
>>> eta = ['8','10', '25']
>>> MAX = 20
>>> for i in range(3):
m_str = '{0}: {1}'.format(line[i], station[i]) #aligns left
m_str = m_str + ' '*(MAX-len(str)-len(eta[i])) + eta[i] #aligns right
print m_str
计算将得到最大长度(在本例中为 20)减去 m_str 的当前 len
减去即将到来的长度(len(eta[i])
)。
请记住,它假定此时 len(m_str)
不会大于 20。
输出:
8: station A 8
45: long station 10
1: great station 25
您还可以在给定字符串上使用 .rjust()
和 .ljust()
方法来设置它们的对齐方式,
lst = [[8, "station A", 8], [45, "long station", 10], [1, "great station", 25]]
for i in lst:
print str(i[0]).ljust(2)+":"+i[1]+str(i[2]).rjust(20 - (len(i[1])+3))
输出:
8 :station A 8
45:long station 10
1 :great station 25
对左侧部分及其长度使用临时字符串:
tmp = '{}: {}'.format(line, station)
print('{}{:{}}'.format(tmp, eta, 20-len(tmp)))
演示:
trains = ((8, 'station A', 8), (45, 'long station', 10), (1, 'great station', 25))
for line, station, eta in trains:
tmp = '{}: {}'.format(line, station)
print('{}{:{}}'.format(tmp, eta, 20-len(tmp)))
Prints:
8: station A 8
45: long station 10
1: great station 25
我觉得你想创建一个 table,所以我建议你使用 prettytable
像这样:
from prettytable import PrettyTable
table = PrettyTable(['line', 'station', 'eta'])
table.add_row([8, 'station A', 10])
table.add_row([6, 'station B', 20])
table.add_row([5, 'station C', 15])
因为它没有内置到 Python,您需要自己从包索引 here.
安装它
我是 Python 的新手,我尝试格式化一个字符串以在 LCD 显示器上输出。
我想输出火车出发的格式化table
- 显示屏固定长度为 20 个字符 (20x4)
- 我有 3 个可变长度的字符串变量(线路、站点、eta)
- 其中 2 个应左对齐(线路、车站),而第三个应右对齐
示例:
8: station A 8
45: long station 10
1: great station 25
我玩过各种东西,但我无法定义整个字符串的最大长度,但只能定义 1 个变量:
print('{0}: {1} {2:<20}'.format(line, station, eta))
非常感谢任何提示和提示!
--- 解决方案基于@Rafael Cardoso 的回答:
print(format_departure(line, station, eta))
def format_departure(line, station, eta):
max_length = 20
truncate_chars = '..'
# add a leading space to the eta - just to be on the safe side
eta = ' ' + eta
output = '{0}: {1}'.format(line, station) # aligns left
# make sure that the first part is not too long, otherwise truncate
if (len(output + eta)) > max_length:
# shorten for truncate_chars + eta + space
output = output[0:max_length - len(truncate_chars + eta)] + truncate_chars
output = output + ' '*(max_length - len(output) - len(eta)) + eta # aligns right
return output
您可以使用切片来指定最大长度。例如。以下将仅打印格式产生的字符串的前 20 个字符:
print('{0}: {1} {2:<20}'.format(line, station, eta)[:20])
您可以通过计算在 station 和 eta 之间应添加多少个空格来添加空格:
>>> line = ['8', '45', '1']
>>> station = ['station A', 'long station', 'great station']
>>> eta = ['8','10', '25']
>>> MAX = 20
>>> for i in range(3):
m_str = '{0}: {1}'.format(line[i], station[i]) #aligns left
m_str = m_str + ' '*(MAX-len(str)-len(eta[i])) + eta[i] #aligns right
print m_str
计算将得到最大长度(在本例中为 20)减去 m_str 的当前 len
减去即将到来的长度(len(eta[i])
)。
请记住,它假定此时 len(m_str)
不会大于 20。
输出:
8: station A 8
45: long station 10
1: great station 25
您还可以在给定字符串上使用 .rjust()
和 .ljust()
方法来设置它们的对齐方式,
lst = [[8, "station A", 8], [45, "long station", 10], [1, "great station", 25]]
for i in lst:
print str(i[0]).ljust(2)+":"+i[1]+str(i[2]).rjust(20 - (len(i[1])+3))
输出:
8 :station A 8
45:long station 10
1 :great station 25
对左侧部分及其长度使用临时字符串:
tmp = '{}: {}'.format(line, station)
print('{}{:{}}'.format(tmp, eta, 20-len(tmp)))
演示:
trains = ((8, 'station A', 8), (45, 'long station', 10), (1, 'great station', 25))
for line, station, eta in trains:
tmp = '{}: {}'.format(line, station)
print('{}{:{}}'.format(tmp, eta, 20-len(tmp)))
Prints:
8: station A 8
45: long station 10
1: great station 25
我觉得你想创建一个 table,所以我建议你使用 prettytable
像这样:
from prettytable import PrettyTable
table = PrettyTable(['line', 'station', 'eta'])
table.add_row([8, 'station A', 10])
table.add_row([6, 'station B', 20])
table.add_row([5, 'station C', 15])
因为它没有内置到 Python,您需要自己从包索引 here.
安装它