python格式输出占四列左对齐_python – 打印字符串左对齐,固定宽度和后缀
您不能更改“”.format(),因为它是内置的,但如果可以为方法提供字符串和参数:
print(kf.format("{:t{}} {}", key, ':', value))
您可以通过继承string.Formatter来允许空格式字段并提供特殊类型处理程序来实现:
from string import Formatter
import sys
if sys.version_info < (3,):
int_type = (int, long)
else:
int_type = (int)
class TrailingFormatter(Formatter):
def vformat(self, *args):
self._automatic = None
return super(TrailingFormatter, self).vformat(*args)
def get_value(self, key, args, kwargs):
if key == '':
if self._automatic is None:
self._automatic = 0
elif self._automatic == -1:
raise ValueError("cannot switch from manual field specification "
"to automatic field numbering")
key = self._automatic
self._automatic += 1
elif isinstance(key, int_type):
if self._automatic is None:
self._automatic = -1
elif self._automatic != -1:
raise ValueError("cannot switch from automatic field numbering "
"to manual field specification")
return super(TrailingFormatter, self).get_value(key, args, kwargs)
def format_field(self, value, spec):
if len(spec) > 1 and spec[0] == 't':
value = str(value) + spec[1] # append the extra character
spec = spec[2:]
return super(TrailingFormatter, self).format_field(value, spec)
kf = TrailingFormatter()
w = 20
ch = ':'
x = dict(a_key=23, another_key=42)
for k in sorted(x):
v = x[k]
print(kf.format('{:t{}
给你:
a_key: 23
another_key: 42
您当然可以对ch和w值进行硬编码:
print(kf.format('{:t:<20} {}', k, v))
为了更好的可读性,但灵活性较差.
Python 3.4 string.Formatter()的backport包含版本(至少)高达3.5.0rc1的错误修复,包含此代码现在可在PyPI上获得
python格式输出占四列左对齐_python – 打印字符串左对齐,固定宽度和后缀相关推荐
- python中右对齐_Python rjust()字符串右对齐方法详解
和ljust() 方法类似,不同之处在于,rjust() 方法是向字符串的左侧填充指定字符,从而达到右对齐文本的目的. rjust() 方法的基本格式如下: S.rjust(width[, fillc ...
- python字符串居中对齐_Python center()字符串居中对齐方法详解
center() 字符串方法与ljust()和rjust()的用法类似,唯一的不同在于,该方法的功能是让字符串居中,而不是左对齐或右对齐. center() 方法的基本格式如下: S.center(w ...
- python中右对齐_python中如何右对齐-问答-阿里云开发者社区-阿里云
例如,有一个字典如下: dic = { "name": "botoo", "url": "http://www.123.com&q ...
- python格式化输出宽度对齐_python 中 字符串格式化示例,输出格式没有达到指定宽度,求帮忙!...
python 版本:2.7.3 运行平台:macX 10.8 开发工具:IDLE 问题描述 width = input('Please enter width(>10): ') price_wi ...
- python表格对齐_python str.format 中文对齐的细节问题,
python str.format 中文对齐的细节问题, 写了一个练手的爬虫...在输出的时候出现了让人很不愉♂悦的问题 像这样: 令人十分难受啊! #------------------------ ...
- python二维数据读取对齐_python代码要严格对齐
此课程与<清华编程高手.尹成.带你实战python入门>大体相同,只需购买其中的一门课程. 本课程由清华大学尹成老师录制,课程的特色在于讲解原理的同时引入了每个程序员都热衷的黑客技术.py ...
- python字符串字面量有哪四种定义方式_python中字符串连接的四种方式
原博文 2018-12-05 14:28 − 以下实例展示了join()的使用方法 #!/usr/bin/python str = "-"; seq = ("a" ...
- python循环输出花式图案锁屏教程_Python仅用3行代码就能输出花式字符串图集,同事直呼666!...
高逼格的日志 相信Java程序员看到上面的图,一定不会陌生.没错,springboot的启动日志.不知道其他人怎么想,我第一次看到这个启动日志的时候,就觉得好炫酷.然而,大家在日常的Python开发中 ...
- python数据框添加一列无列名_Python将列添加到数据框导致NaN
我有一个系列和df s = pd.Series([1,2,3,5]) df = pd.DataFrame() 当我像这样向df添加列时 df.loc[:, "0-2"] = s.i ...
最新文章
- usaco Overfencing 穿越栅栏(BFS)
- 条件随机场概率无向图模型的因子分解
- 揭秘人工智能面试内容:8家国际巨头机器学习面试题目整理
- OpenGL 位移贴图实例
- 《大型网站技术架构》读书笔记二:大型网站架构模式
- 【转】Android 最火的快速开发框架XUtils
- 2019计算机视觉领域顶级会议时间表
- 【百度echarts】实现圆环进度条-代码示例代码demo
- lighttpd 之防盗链
- android asmack调用MultiUserChat.getHostedRooms方法出现空指针的异常解决方案
- 机器视觉行业的很多知识
- Rstudio 1.2 新功能介绍
- Tornado 一些资料
- 为什么html字体之间有间隔,css字体间隔怎么调整?css文字间隔的调整方法
- wnidows查看电脑序列号命令
- Exception in thread “main“ java.lang.ArithmeticException: / by zer
- 强制修改服务器时间,修改服务器时间脚本
- 对贴片陶瓷电容材料的区分
- 网络安全没有“银弹”
- QT日历 QCalendarWidget
热门文章
- 微课|中学生可以这样学Python(例11.3):tkinter通信录管理系统3
- Python+pywin32批量转换Word文件为PDF文件
- Python内置函数sorted()和列表方法sort()排序规则不得不说的事
- php 正则表达式获取html标签内容_总结Python网络爬虫四大选择器(正则表达式、BS4、Xpath、CSS)...
- android 属性动画 补间动画,每日一道面试题(第7期)---Android补间动画与属性动画的区别...
- python内置高阶函数求导_Python——函数式编程、高阶函数和内置函数,及
- python灰度图生成g代码_Python打造一个在线G代码生成器
- oracle升级12.2,Oracle 11.2.0.4升级到12.2.0.1
- java fst 入门 例子,Java FST.save方法代码示例
- php远程simplexml_load_string()出错,PHP simplexml_load_string无法正常工作