[英]Custom string interpolation in Python
I want to create a custom f-string.我想创建一个自定义 f 字符串。 For example, a CPT
interpolator that always converts things it formats to capital letters:例如,一个CPT
插值器总是将它格式化的东西转换为大写字母:
a = "World"
normal_f_string = f"Hello {a}" # "Hello World"
my_custom_interpolator = CPT"Hello {a}" # "Hello WORLD"
I find the answer on Trigger f-string parse on python string in variable .我在变量中的 python 字符串上的 Trigger f-string parse 上找到了答案。 Specifically, here is the code adapted to my question:具体来说,这是适用于我的问题的代码:
from string import Formatter
import sys
def idem(x):
return x
_conversions = {'a': ascii, 'r': repr, 's': str, 'e': idem}
# 'e' is new, for cancelling capitalization. Note that using any conversion has this effect, e is just doesn't do anything else.
def z(template, locals_=None):
if locals_ is None:
previous_frame = sys._getframe(1)
previous_frame_locals = previous_frame.f_locals
locals_ = previous_frame_locals
# locals_ = globals()
result = []
parts = Formatter().parse(template)
for part in parts:
literal_text, field_name, format_spec, conversion = part
if literal_text:
result.append(literal_text)
if not field_name:
continue
value = eval(field_name, locals_) #.__format__()
if conversion:
value = _conversions[conversion](value)
if format_spec:
value = format(value, format_spec)
else:
value = str(value)
if not conversion:
value = value.upper() # Here we capitalize the thing.
result.append(value)
res = ''.join(result)
return res
# Usage:
a = 'World'
b = 10
z('Hello {a} --- {a:^30} --- {67+b} --- {a!r}')
a = "World"
normal_f_string = f"Hello {a}" # "Hello World"
my_custom_interpolator = lambda x: f"Hello {x.upper()}"
print(my_custom_interpolator(a))
Gives:给出:
Hello WORLD
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.