Skip to content

Commit

Permalink
feat: 封装str.just函数以适配中文字符
Browse files Browse the repository at this point in the history
  • Loading branch information
weiduhuo committed Oct 18, 2023
1 parent 91a3b5a commit 0c34805
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions utils/calculated.py
Original file line number Diff line number Diff line change
Expand Up @@ -1026,4 +1026,16 @@ def __init__(self, arr:np.ndarray, key_index:int = -1, value_index:int = None):

def __getitem__(self, key):
return self.data_dict[key]


def str_just(text:str, width:int, left = True):
"""
说明:
封装str.rjust()&str.ljust(),以适应中文字符的实际宽度
"""
ch_cnt = (len(text.encode('utf-8')) - len(text)) // 2 # 中文字符的个数
if left:
return text.ljust(width-ch_cnt)
else:
return text.rjust(width-ch_cnt)

0 comments on commit 0c34805

Please sign in to comment.