Python 字符串格式化 - 限制字符串长度,但修剪字符串开头
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26531900/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python string formatting - limit string length, but trim string beginning
提问by ?ukasz Rogalski
I'm using Python standard logging module with custom formatter where I limit length of some fields. It uses standard %Python operator.
我正在使用带有自定义格式化程序的 Python 标准日志记录模块,其中我限制了某些字段的长度。它使用标准的%Python 运算符。
I can apply limit for percent-formatted string like this (this limits length to 10 chars):
我可以像这样对百分比格式的字符串应用限制(这将长度限制为 10 个字符):
>>> "%.10s" % "Lorem Ipsum"
'Lorem Ipsu'
Is it possible to trim it from the beginning, so the output is 'orem Ipsum'(without manipulating right-side argument)?
是否可以从一开始就修剪它,所以输出是'orem Ipsum'(不操作右侧参数)?
采纳答案by Aaron Hall
Is it possible to trim it from the beginning with % formatting?
是否可以使用 % 格式从头开始修剪它?
Python's % formatting comes from C's printf.
Note that the .indicates precision for a float. That it works on a string is a mere side effect, and unfortunately, there is no provision in the string formatting specification to accommodate stripping a string from the leftto a fixed max width.
请注意,.指示浮点数的精度。它适用于字符串只是一个副作用,不幸的是,字符串格式规范中没有规定将字符串从左侧剥离到固定的最大宽度。
Therefore if you must strip a string to a fixed width from the end, I recommend to slice from a negative index. This operation is robust, and won't fail if the string is less than 10 chars.
因此,如果您必须将字符串从末尾剥离到固定宽度,我建议从负索引切片。此操作是健壮的,如果字符串少于 10 个字符,则不会失败。
>>> up_to_last_10_slice = slice(-10, None)
>>> 'Lorem Ipsum'[up_to_last_10_slice]
'orem Ipsum'
>>> 'Ipsum'[up_to_last_10_slice]
'Ipsum'
str.formatalso no help
str.format也没有帮助
str.formatis of no help here, the width is a minimum width:
str.format在这里没有帮助,宽度是最小宽度:
>>> '{lorem:>10}'.format(lorem='Lorem Ipsum')
'Lorem Ipsum'
>>> '{lorem:*>10}'.format(lorem='Lorem')
'*****Lorem'
(The asterisk, "*", is the fill character.)
(星号“ *”是填充字符。)
回答by Abhijit
This can easily be done through slicing, so you do not require any string format manipulation to do your JOB
这可以通过切片轻松完成,因此您不需要任何字符串格式操作来完成您的工作
>>> "Lorem Ipsum"[-10:]
'orem Ipsum'
回答by Martin Wallgren
I had the same question and came up with this solution using LogRecordFactory.
我有同样的问题,并使用 LogRecordFactory 提出了这个解决方案。
orig_factory = logging.getLogRecordFactory()
def record_factory(*args, **kwargs):
record = orig_factory(*args, **kwargs)
record.sname = record.name[-10:] if len(
record.name) > 10 else record.name
return record
logging.setLogRecordFactory(record_factory)
Here I am truncating the name to 10 characters and storing it in the attribute sname, which can be used as any other value.
在这里,我将名称截断为 10 个字符并将其存储在属性 sname 中,该属性可以用作任何其他值。
%(sname)10s
It is possible to store the truncated name in record.name, but I wanted to keep the original name around too.
可以将截断的名称存储在 record.name 中,但我也想保留原始名称。

