python 中英文对齐
代码
def get_len(string: str):
length = 0
for ch in string:
if '\u4e00' <= ch <= '\u9fa5': # 是中文字符
length += 2
else:
length += 1
return length
def aligned(string: str, length=10, align_type='<') -> str:
"""
:param string: 要对齐的字符串
:param length: 要对齐的长度
:param align_type: 要对齐的类型
:return: 对齐后的字符串 用空格补充空白
"""
len_s = get_len(string)
if len_s < length:
if align_type == '>':
show = ' ' * (length - len_s) + string
elif align_type == '<':
show = string + ' ' * (length - len_s)
else:
left = int((length - len_s) / 2)
show = ' ' * left + string + ' ' * (length - left - len_s)
else:
raise RuntimeError(f"给定的length应该大于string的长度, {length}<->{string}({len(string)})")
return show
if __name__ == '__main__':
data = [['ip', 'core', 'disk', 'memory'],
['172.17.11.1', '128', '12349', '50G'],
['172.17.11.1', '2654', '1234984', '150G'],
['172.17.11.1', '128', '1234933433484', '10240G']]
for i in data:
s = aligned(i[0], 20) + aligned(i[1], 20) + aligned(i[2], 20) + aligned(i[3], 20)
print(s)
"""
ip core disk memory
172.17.11.1 128 12349 50G
172.17.11.1 2654 1234984 150G
172.17.11.1 128 1234933433484 10240G
"""
print("*"*100)
data = [['这是IP', '这是核心数core', '这是磁盘disk', '这是内存memory'],
['172.17.11.1', '128', '12349', '50G'],
['172.17.11.1', '2654', '1234984', '150G'],
['172.17.11.1', '128', '1234933433484', '10240G']]
for i in data:
s = aligned(i[0], 20) + aligned(i[1], 20) + aligned(i[2], 20) + aligned(i[3], 20)
# print('"{}""{}""{}""{}"'.format(aligned(i[0], 20),aligned(i[1], 20),aligned(i[2], 20),aligned(i[3], 20)))
print(s)
"""
ip core disk memory
172.17.11.1 128 12349 50G
172.17.11.1 2654 1234984 150G
172.17.11.1 128 1234933433484 10240G
"""
运行
在cmd中和Linux中都能对齐
但是在pycharm中对不齐