当前位置:网站首页>String alignment method, self use, synthesis, newrlcjust
String alignment method, self use, synthesis, newrlcjust
2022-07-05 07:28:00 【work-harder】
background
- The content format of multiple strings in the list is inconsistent , There is pure Chinese , Pure English , There is also a mixture of Chinese and English .
- Use python Built-in function str.rjust Isochronous , Unable to align
- win10, anaconda 4.8.3, python 3.8.3
Way
- Calculate the middle of each string 、 Number of English single characters , Adjustment
- See code for details
- Fill character ( Fill in spaces to keep the length of characters ) Only single byte characters are supported temporarily
#
# pure English , Pure Chinese , A list of mixed English and Chinese strings (list) Align content : Right , Left , in
# After import , function chn_en_alignment.test() See how to use
# After import , function chn_en_alignment.newrlcjust(listchk, ttllength, direction ='r', fillwith = '0')
# or import chn_en_alignment.newrlcjust as rlcjust, Again according to rlcjust(' The order of the above parameters ') function
# V1, 20201016
#
# data for test
list1=['abc', 'a', 'abcded', 'aa']
list2=[' Middle core ', ' TSMC ', ' Huawei Technology ']
list3=['NXP', ' Middle core ', ' TSMC ']
list4=['NXP', ' Middle core ', 'Huawei Technology ']
list5=['123', ' in " core "', ' Hua Hua by Wei']
usage=''' Usage: newrlcjust(listchk, ttllength, direction ='r', fillwith = '0') listchk: String element list, To be checked ttllength: Integers , list The width requirements of all string alignment in , ttllength: The numbers are ttllength And list Maximum length of Chinese character conversion The biggest of direction: r-right, l-left, c-center, Default r fillwith: What to fill the empty space with ( If there is ), Default 0 ( zero ), Non English or non single byte characters are not supported at the moment '''
# chn =2, en=1, return additional len
def getLen(strchk):
# get total length, chn calculated as 2, en as 1
strlen = 0
for letter in strchk:
# print(letter, flush=True)
if ord(letter) > 255:
strlen += 2
else:
strlen += 1
return strlen
def newrlcjust(listchk, ttllength, direction ='r', fillwith = '0'):
# listchk: list with elements to be aligned;
# ttllength: total length you see after output;
# direction, default as 'r', one of the three 'r', or 'l', or 'c',
# fillwith, default as '0';
listchkLenmax=max(getLen(x) for x in listchk)
for strchk in listchk:
difflength = 0
if ttllength > listchkLenmax:
listchkLenmax = ttllength
ttl = listchkLenmax - (getLen(strchk)-len(strchk))
if direction == 'r':
print(strchk.rjust(ttl, fillwith))
elif direction == 'l':
print(strchk.ljust(ttl, fillwith))
else:
print(strchk.center(ttl, fillwith))
def test():
print('\n Chinese numeration 2 Width , English numeration 1 Width .')
print('len(str) Middle 、 A single part of English , Average position length 1')
print(usage)
print( "*" * 66, '\n')
print('following are examples: \n')
i_sequence = 0
for lst in [list1, list2, list3, list4, list5]:
print('lst:',lst)
direction=['r','l','c'][i_sequence]
print("example as: newrlcjust(lst, ttllength=1, direction ='" +direction + "', fillwith = '0')")
newrlcjust(lst, ttllength=1, direction =direction, fillwith = '0')
print('\n')
i_sequence += 1
i_sequence = i_sequence % 3
if __name__ == '__main__':
# newrlcjust(list4, 20, direction ='r', fillwith = '…')
# fill Of … , Chinese input method shift + 6, Again backspace once . powershell Cannot enter under
test()
边栏推荐
- Batch convert txt to excel format
- Miracast技术详解(一):Wi-Fi Display
- Database SQL practice 3. Find the current salary details of the current leaders of each department and their corresponding department number Dept_ no
- [untitled]
- [solved] there is something wrong with the image
- Course learning accumulation ppt
- [software testing] 03 -- overview of software testing
- Database SQL practice 4. Find the last of employees in all assigned departments_ Name and first_ name
- And play the little chestnut of dynamic agent
- 氢氧化钠是什么?
猜你喜欢
[idea] efficient plug-in save actions to improve your work efficiency
SOC_ SD_ CMD_ FSM
How to deal with excessive memory occupation of idea and Google browser
Steps and FAQs of connecting windows Navicat to Alibaba cloud server MySQL
And play the little chestnut of dynamic agent
C#学习笔记
[software testing] 04 -- software testing and software development
【Node】nvm 版本管理工具
2022年PMP项目管理考试敏捷知识点(7)
The mutual realization of C L stack and queue in I
随机推荐
selenium 元素定位
SD_ CMD_ SEND_ SHIFT_ REGISTER
Selenium element positioning
Concurrent programming - deadlock troubleshooting and handling
目标检测系列——Faster R-CNN原理详解
Qu'est - ce que l'hydroxyde de sodium?
Idea shortcut key
Eclipse project recompile, clear cache
[idea] efficient plug-in save actions to improve your work efficiency
NPM and package common commands
行测--资料分析--fb--高照老师
The problem of configuring opencv in qt5.13.2 is solved in detail
Anaconda navigator click open no response, can not start error prompt attributeerror: 'STR' object has no attribute 'get‘
UE5热更新-远端服务器自动下载和版本检测(SimpleHotUpdate)
Binary search (half search)
[vscode] prohibit the pylance plug-in from automatically adding import
[untitled]
氢氧化钠是什么?
The difference between NPM install -g/-save/-save-dev
【无标题】