Python print输出对齐的问题——解决中文对齐问题
2022-09-18
首先,先简单介绍一下,print自己有对齐的方式
下面的,就是给定字符长度10个字符长度,对齐方式为左边对齐,空余的字符采用 * 填充
string = "1234567" print(string.ljust(10,"*"))
当然了,我整理了三个方式,左右中,一起打印出来了
string = "1234567" print(string.ljust(10,"*"))#左 print(string.center(10,"*"))#中 print(string.rjust(10,"*"))#右
综上就会有一个不错的打印结果如下:
print("\n\n") print("Name".ljust(10," "),end="") print("Price".ljust(10," "),end="") print("") print("Noodle".ljust(10," "),end="") print("10RMB".ljust(10," "),end="") print("") print("Meat".ljust(10," "),end="") print("40RMB".ljust(10," "),end="") print("\n\n")
但是当你将字符换为中文的时候,你就会发现它的结果就不那么尽人意了
print("\n\n") print("名字".ljust(10," "),end="") print("价格".ljust(10," "),end="") print("") print("面条".ljust(10," "),end="") print("10元".ljust(10," "),end="") print("") print("肉".ljust(10," "),end="") print("40元".ljust(10," "),end="") print("\n\n")
原因很简单,因为中文字符默认是全角输出,而英文字符默认为半角, 半角和全角就像英文的 "," 和中文的 "," ,中文的逗号后面会更出一个小空格来,所以造成了这样的不规则
解决方法:
将非半角全部转换为半角,话不多说函数如下,调用方法我写在函数的最顶端了
''' 调用方法 #左边对齐 string1 = "字符串123" result = duiqi(string1,10,"left")#对齐字符串,长度,对齐方式 print(result) #右边对齐 string1 = "字符串123" result = duiqi(string1,10,"right")#对齐字符串,长度,对齐方式 print(result) #居中对齐这个有点麻烦,例如 “123” ,对齐长度 6 ,要补三个空格,前面一个后面两个还是前面两个后面一个,这个是可以设置的 对齐的方式有 center0 center1 center2 center1:前面的空格更少,后面的更多 center2:后面的空格更少,前面的更多 center0:两边空格一样多(需要字符合理——例: 字符串“123”, 长度为 “5”,不然要报错) ''' def duiqi(string,length,way): if(way == "left"): difference = length - len(string) if difference == 0: # 若差值为0则不需要补 return string elif difference < 0: print('错误:限定的对齐长度小于字符串长度!') return None new_string = '' space = ' ' for i in string: codes = ord(i) # 将字符转为ASCII或UNICODE编码 if codes <= 126: # 若是半角字符 new_string = new_string + chr(codes+65248) # 则转为全角 else: new_string = new_string + i # 若是全角,则不转换 return new_string + space*(difference) # 返回补齐空格后的字符串 elif(way == "right"): difference = length - len(string) if difference == 0: # 若差值为0则不需要补 return string elif difference < 0: print('错误:限定的对齐长度小于字符串长度!') return None new_string = '' space = ' ' for i in string: codes = ord(i) # 将字符转为ASCII或UNICODE编码 if codes <= 126: # 若是半角字符 new_string = new_string + chr(codes+65248) # 则转为全角 else: new_string = new_string + i # 若是全角,则不转换 return space*(difference) + new_string # 返回补齐空格后的字符串 elif(way == "center0"): difference = length - len(string) if difference == 0: # 若差值为0则不需要补 return string elif difference < 0: print('错误:限定的对齐长度小于字符串长度!') return None new_string = '' space = ' ' for i in string: codes = ord(i) # 将字符转为ASCII或UNICODE编码 if codes <= 126: # 若是半角字符 new_string = new_string + chr(codes+65248) # 则转为全角 else: new_string = new_string + i # 若是全角,则不转换 return space*(int(difference/2)) + new_string + space*(int(difference/2)) # 返回补齐空格后的字符串 elif(way == "center2"): difference = length - len(string) if difference == 0: # 若差值为0则不需要补 return string elif difference < 0: print('错误:限定的对齐长度小于字符串长度!') return None new_string = '' space = ' ' for i in string: codes = ord(i) # 将字符转为ASCII或UNICODE编码 if codes <= 126: # 若是半角字符 new_string = new_string + chr(codes+65248) # 则转为全角 else: new_string = new_string + i # 若是全角,则不转换 return space*(int(difference/2+1)) + new_string + space*(int(difference/2)) elif(way == "center1"): difference = length - len(string) if difference == 0: # 若差值为0则不需要补 return string elif difference < 0: print('错误:限定的对齐长度小于字符串长度!') return None new_string = '' space = ' ' for i in string: codes = ord(i) # 将字符转为ASCII或UNICODE编码 if codes <= 126: # 若是半角字符 new_string = new_string + chr(codes+65248) # 则转为全角 else: new_string = new_string + i # 若是全角,则不转换 return space*(int(difference/2)) + new_string + space*(int(difference/2 + 1)) # 返回补齐空格后的字符串 elif(way == "center2"): difference = length - len(string) if difference == 0: # 若差值为0则不需要补 return string elif difference < 0: print('错误:限定的对齐长度小于字符串长度!') return None new_string = '' space = ' ' for i in string: codes = ord(i) # 将字符转为ASCII或UNICODE编码 if codes <= 126: # 若是半角字符 new_string = new_string + chr(codes+65248) # 则转为全角 else: new_string = new_string + i # 若是全角,则不转换 return space*(int(difference/2+1)) + new_string + space*(int(difference/2)) # 返回补齐空格后的字符串
那么使用了上面的函数后,运行的代码截图就应该为
代码如下:
''' 调用方法 #左边对齐 string1 = "字符串123" result = duiqi(string1,10,"left")#对齐字符串,长度,对齐方式 print(result) #右边对齐 string1 = "字符串123" result = duiqi(string1,10,"right")#对齐字符串,长度,对齐方式 print(result) #居中对齐这个有点麻烦,例如 “123” ,对齐长度 6 ,要补三个空格,前面一个后面两个还是前面两个后面一个,这个是可以设置的 对齐的方式有 center0 center1 center2 center1:前面的空格更少,后面的更多 center2:后面的空格更少,前面的更多 center0:两边空格一样多(需要字符合理——例: 字符串“123”, 长度为 “5”,不然要报错) ''' def duiqi(string,length,way): if(way == "left"): difference = length - len(string) if difference == 0: # 若差值为0则不需要补 return string elif difference < 0: print('错误:限定的对齐长度小于字符串长度!') return None new_string = '' space = ' ' for i in string: codes = ord(i) # 将字符转为ASCII或UNICODE编码 if codes <= 126: # 若是半角字符 new_string = new_string + chr(codes+65248) # 则转为全角 else: new_string = new_string + i # 若是全角,则不转换 return new_string + space*(difference) # 返回补齐空格后的字符串 elif(way == "right"): difference = length - len(string) if difference == 0: # 若差值为0则不需要补 return string elif difference < 0: print('错误:限定的对齐长度小于字符串长度!') return None new_string = '' space = ' ' for i in string: codes = ord(i) # 将字符转为ASCII或UNICODE编码 if codes <= 126: # 若是半角字符 new_string = new_string + chr(codes+65248) # 则转为全角 else: new_string = new_string + i # 若是全角,则不转换 return space*(difference) + new_string # 返回补齐空格后的字符串 elif(way == "center0"): difference = length - len(string) if difference == 0: # 若差值为0则不需要补 return string elif difference < 0: print('错误:限定的对齐长度小于字符串长度!') return None new_string = '' space = ' ' for i in string: codes = ord(i) # 将字符转为ASCII或UNICODE编码 if codes <= 126: # 若是半角字符 new_string = new_string + chr(codes+65248) # 则转为全角 else: new_string = new_string + i # 若是全角,则不转换 return space*(int(difference/2)) + new_string + space*(int(difference/2)) # 返回补齐空格后的字符串 elif(way == "center2"): difference = length - len(string) if difference == 0: # 若差值为0则不需要补 return string elif difference < 0: print('错误:限定的对齐长度小于字符串长度!') return None new_string = '' space = ' ' for i in string: codes = ord(i) # 将字符转为ASCII或UNICODE编码 if codes <= 126: # 若是半角字符 new_string = new_string + chr(codes+65248) # 则转为全角 else: new_string = new_string + i # 若是全角,则不转换 return space*(int(difference/2+1)) + new_string + space*(int(difference/2)) elif(way == "center1"): difference = length - len(string) if difference == 0: # 若差值为0则不需要补 return string elif difference < 0: print('错误:限定的对齐长度小于字符串长度!') return None new_string = '' space = ' ' for i in string: codes = ord(i) # 将字符转为ASCII或UNICODE编码 if codes <= 126: # 若是半角字符 new_string = new_string + chr(codes+65248) # 则转为全角 else: new_string = new_string + i # 若是全角,则不转换 return space*(int(difference/2)) + new_string + space*(int(difference/2 + 1)) # 返回补齐空格后的字符串 elif(way == "center2"): difference = length - len(string) if difference == 0: # 若差值为0则不需要补 return string elif difference < 0: print('错误:限定的对齐长度小于字符串长度!') return None new_string = '' space = ' ' for i in string: codes = ord(i) # 将字符转为ASCII或UNICODE编码 if codes <= 126: # 若是半角字符 new_string = new_string + chr(codes+65248) # 则转为全角 else: new_string = new_string + i # 若是全角,则不转换 return space*(int(difference/2+1)) + new_string + space*(int(difference/2)) # 返回补齐空格后的字符串 print("\n\n") print(duiqi("名字",10,"left"),end="") print(duiqi("价格",10,"left"),end="") print("") print(duiqi("面条",10,"left"),end="") print(duiqi("10RMB",10,"left"),end="") print("") print(duiqi("肉",10,"left"),end="") print(duiqi("40RMB",10,"left"),end="") print("\n\n")
发表评论: