python文本处理,format方法--转子网上 crifan
原文出处:https://www.crifan.com/python_string_format_fill_with_chars_and_set_alignment/
【问题】
想要获得这样的效果:
| 
 ——-abc   | 
【解决过程】
1.折腾半天,终于从Python的手册中,找到对应的用法了。
完整代码如下:
| 
 1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 
28 
29 
30 
31 
32 
33 
34 
35 
36 
37 
38 
39 
40 
41 
42 
43 
44 
45 
46 
 | 
#!/usr/bin/python# -*- coding: utf-8 -*-"""Function:【已解决】Python中,带填充和设置对齐方式的,格式化字符串输出Author:     Crifan LiVersion:    2012-12-26Contact:    admin at crifan dot com"""def printFillWith():    inputStrList = [        "abc",        "abcd",        "abcde",    ];         #refer:    #Python 2.7.3 Manual ->    #7.1.3.1. Format Specification Mini-Language    #7.1.3.2. Format examples         for eachStr in inputStrList:        #print '{:->10}'.format(eachStr);        print '{0:->10}'.format(eachStr);        # -------abc        # ------abcd        # -----abcde    for eachStr in inputStrList:        print '{0:-<20}'.format(eachStr);        # abc-----------------        # abcd----------------        # abcde---------------         for eachStr in inputStrList:        print '{0:*^30}'.format(eachStr);        # *************abc**************        # *************abcd*************        # ************abcde*************if __name__ == "__main__":    printFillWith(); | 
【总结】
总的来说就是类似于:
| ‘{0:-<20}’.format(eachStr); | 
的写法,即可。
更多参数的含义,摘录手册中的部分解释,如下:
format_spec ::= [[fill]align][sign][#][0][width][,][.precision][type]
fill ::= <a character other than ‘}’>
align ::= "<" | ">" | "=" | "^"
sign ::= "+" | "-" | " "
width ::= integer
precision ::= integer
type ::= "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%"
align:
Option Meaning '<' Forces the field to be left-aligned within the available space (this is the default for most objects). '>' Forces the field to be right-aligned within the available space (this is the default for numbers). '=' Forces the padding to be placed after the sign (if any) but before the digits. This is used for printing fields in the form ‘+000000120’. This alignment option is only valid for numeric types. '^' Forces the field to be centered within the available space. sign:
Option Meaning '+' indicates that a sign should be used for both positive as well as negative numbers. '-' indicates that a sign should be used only for negative numbers (this is the default behavior). space indicates that a leading space should be used on positive numbers, and a minus sign on negative numbers. 对于字符string:
Type Meaning 's' String format. This is the default type for strings and may be omitted. None The same as 's'. 对于数字integer :
Type Meaning 'b' Binary format. Outputs the number in base 2. 'c' Character. Converts the integer to the corresponding unicode character before printing. 'd' Decimal Integer. Outputs the number in base 10. 'o' Octal format. Outputs the number in base 8. 'x' Hex format. Outputs the number in base 16, using lower- case letters for the digits above 9. 'X' Hex format. Outputs the number in base 16, using upper- case letters for the digits above 9. 'n' Number. This is the same as 'd', except that it uses the current locale setting to insert the appropriate number separator characters. None The same as 'd'. 浮点和整数(floating point and decimal ):
Type Meaning 'e' Exponent notation. Prints the number in scientific notation using the letter ‘e’ to indicate the exponent. 'E' Exponent notation. Same as 'e' except it uses an upper case ‘E’ as the separator character. 'f' Fixed point. Displays the number as a fixed-point number. 'F' Fixed point. Same as 'f'. 'g' General format. For a given precision p >= 1, this rounds the number to p significant digits and then formats the result in either fixed-point format or in scientific notation, depending on its magnitude.
The precise rules are as follows: suppose that the result formatted with presentation type 'e' and precision p-1 would have exponentexp. Then if -4 <= exp < p, the number is formatted with presentation type 'f' and precision p-1-exp. Otherwise, the number is formatted with presentation type 'e' and precision p-1. In both cases insignificant trailing zeros are removed from the significand, and the decimal point is also removed if there are no remaining digits following it.
Positive and negative infinity, positive and negative zero, and nans, are formatted as inf, -inf, 0, -0 and nan respectively, regardless of the precision.
A precision of 0 is treated as equivalent to a precision of 1.
'G' General format. Same as 'g' except switches to 'E' if the number gets too large. The representations of infinity and NaN are uppercased, too. 'n' Number. This is the same as 'g', except that it uses the current locale setting to insert the appropriate number separator characters. '%' Percentage. Multiplies the number by 100 and displays in fixed ('f') format, followed by a percent sign. None The same as 'g'. 
python文本处理,format方法--转子网上 crifan的更多相关文章
- Python中格式化format()方法详解
		
Python中格式化format()方法详解 Python中格式化输出字符串使用format()函数, 字符串即类, 可以使用方法; Python是完全面向对象的语言, 任何东西都是对象; 字符串的参 ...
 - Python 字符串格式化操作 - format方法
		
建议使用format()方法 字符串操作 对于 %, 官方以及给出这种格式化操作已经过时,在 Python 的未来版本中可能会消失. 在新代码中使用新的字符串格式.因此推荐大家使用format()来替 ...
 - python的str.format方法
		
format方法被用于字符串的格式化输出. print('{0}+{1}={2}'.format(1,2,1+2)) #in 1+2=3 #out 可见字符串中大括号内的数字分别对应着format的几 ...
 - Python入门之format()方法
		
在此列出format()方法的一些基本使用: >>> '{}{}{}'.format('圆周率是',3.1415926,'...') '圆周率是3.1415926...' >& ...
 - 15.Python文本转化语音方法
		
1.用pywin32模块来将文本转化为语音 通过pip install pywin32安装模块,pywin32是个万金油的模块,太多的场景使用到它,但在文本转语音上,它却是个青铜玩家,简单无脑但效果不 ...
 - Python字符串格式化--format()方法
		
https://blog.csdn.net/i_chaoren/article/details/77922939 csdn
 - Python里format()方法基本使用
		
'''第一种:自然连接''' #format 连接字符串 str = '{}使用的python是{}版本'.format('我','3.6.5') print(str) #打印结果:我使用的pytho ...
 - #python str.format 方法被用于字符串的格式化输出。
		
#python str.format 方法被用于字符串的格式化输出. #''.format() print('{0}+{1}={2}'.format(1,2,3)) #1+2=3 可见字符串中大括号内 ...
 - python文本 单独处理每个字符的方法汇总
		
python文本 单独处理字符串每个字符的方法汇总 场景: 用每次处理一个字符的方式处理字符串 方法: 1.使用list(str) >>> a='abcdefg' >&g ...
 
随机推荐
- 【sock_stream和sock_dgram】、 【AF_INET和AF_UNIX】
			
[sock_stream和sock_dgram] 1.sock_stream 是有保障的(即能保证数据正确传送到对方)面向连接的SOCKET,多用于资料(如文件)传送. 2.sock_dgram 是无 ...
 - Python中怎么读写文件
			
python中对文件的操作大概分为三步:打开文件.操作文件(读.写.追加写入).关闭文件. 1.无论对文件做哪种操作,操作前首先要保证文件被打开了,即需要一个打开的操作. 例:open(XXX.txt ...
 - vue.js+webpack在一个简单实例中的使用过程demo
			
这里主要记录vue.js+webpack在一个简单实例中的使用过程 说明:本次搭建基于Win 7平台 Node.js 安装官网提供了支持多种平台的的LTS版本下载,我们根据需要来进行下载安装.对于Wi ...
 - Android中控件之间添加分割线
			
将以下view标签放置在需要分割的两个控件之间: <View android:layout_width=”match_parent” android:layout_height=”1dp” an ...
 - vue刷新路由,不刷新页面
			
1.路由介绍 vue-router是Vue.js官方的路由插件,它和vue.js是深度集成的,适合用于构建单页面应用.vue的单页面应用是基于路由和组件的,路由用于设定访问路径,并将路径和组件映射起来 ...
 - 关于ckeditor5设置高度
			
在管理端模板AdminBSBMaterialDesign-master里发现一个比百度编辑器看起来更高大上的编辑器:ckeditor.模板中使用的是版本4,自己在官网上下载了最新版本.在之前的版本,使 ...
 - element-ui的那些坑与总结
			
tags: 默认情况下,下划线是文本宽度 如果要加宽,则可以设置文本(label)的padding, 常规情况下,无法改label宽度,因为他是动态计算的 不过,可以通过自定义,把label拿出来,自 ...
 - 第三方jar包导入unity
			
关于第三方SDK接入Unity工程方面,有许多坑,下面我把遇到的问题进行总结,希望能够帮到有需要的朋友们.1.把第三方SDK导入Eclipse遇到的问题.eclipse配置完成右键工程后没有andro ...
 - 星星闪烁+多边形移动 canvas
			
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
 - 用户认证:基于jwt和session的区别和优缺点
			
背景知识: Authentication和Authorization的区别: Authentication:用户认证,指的是验证用户的身份,例如你希望以小A的身份登录,那么应用程序需要通过用户名和密码 ...