反向字符串格式化(可能?) [英] Reverse string-formatting (maybe?)

查看:135
本文介绍了反向字符串格式化(可能?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何内置函数或模块具有类似于我的

组成的,未编写的deformat函数,如下所示?我无法想象

太容易写,但可能......


>> template =''我是%s,他%s最后%s。''
values =('''编码'',''编码'' ,''周'')
格式=模板%值
格式化



''我正在编码,他上周编码。''


>> deformat(格式化,模板)



('''编码'',''编码'',''周'')


扩展(为了更好的视觉效果):


>> deformat (''我正在编码,他上周编码。'',''我是%s,而且他的最后一个%s。'')



('''编码'',''编码'',''周'')


它会返回一个字符串元组,因为它无法分辨每个项目的原始类型是什么。

。 >
任何输入?我查看了字符串模块

和re模块的文档,搜索了文档并搜索了这个

组,然后空出来 - 递过来。

解决方案

Dustan写道:


有没有内置的函数或模块的功能类似于我的

组成,未编写的deformat函数如下?我无法想象

太容易写,但可能......


>>> template =''我是%s,他%s最后%s。''
values =('''编码'',''编码'',''周'' )
格式化=模板%值
格式化



''我正在编码,他上周编码。''


>>> deformat(格式化,模板)



(''编码'',''编码'',''周'')


扩展(为了更好的视觉效果):


>>> deformat(''我正在编码,他上周编码。'',''我是%s,他% s
最后%s。'')



('''编码'',''编码'','一周'')


这将返回一个字符串元组,因为它无法分辨每个项目的原始类型是什么

。 />

任何输入?我查看了字符串模块

和re模块的文档,搜索了文档并搜索了这个

组,然后空出来 - 手。



简单但不可靠:


>> import re
template ="我是%s,他%s的最后一个%s。"
values =(" coding"," coded"," ;周")
格式=模板%值
def deformat(格式化,模板):



。 ... r = re.compile("(。*)" .join(template.split("%s")))

.... return r.match(格式化) ).groups()

....


> ;> deformat(格式化,模板)



(''coding'',''coded'',''week' ')


彼得


>>> template =''我是%s,他%s最后%s。''


>>> values =('''编码'',''编码'',''周'')
格式化=模板%值
格式化



''我正在编码,他上周编码。 ''


>>> deformat(格式化,模板)



('''编码'',''编码'',''周'')


扩展(为了更好的视觉效果):


>>> deformat(''我正在编码,他上周编码。'',''我是%s,并且他%s最后%s。'')



('''编码'',''编码'',''周'')


它会返回一个字符串元组,因为它无法告诉什么

o每个项目的严格类型是。


任何输入?我查看了字符串模块

和re模块的文档,搜索了文档并搜索了这个

组,然后空出来 - 手。



是的,在您提供的简单案例中,可以使用re模块轻松完成



>> import re
template =''我是%s,他%s最后%s。''
values =('''编码'',''编码'',''周'')
格式=模板%值
unformat_re = re.escape(模板).replace(''%s'',''(。*)'')
#unformat_re = unformat_re.replace(''%i'',''([0-9] +)'' )
r = re.compile(unformat_re)
r.match(格式化).groups()



(''编码'',''编码'',''周'')


当你有像


>> answer =''将值格式化为strin g''
template =''格式化字符串%% s用于%s''%answer





>> template =''该值为%0 *。* f ''
values =(10,4,3.14159)
formatted = template%values
格式化



''值为00003.1415''




>> template =''亲爱的%(姓名)s,谢谢你的%(礼物)。它



非常%(adj)。''%{''name'':'''奶奶'',''礼物'':''毛衣'',''adj'':

''很好''}


此外,当事情变得有点纠结替换

值重复模板中的事项。如果没有格式化,那么我累了,而且他不会持续所有的圣日。将

解析为(''累'',没有上次,所有圣日)或

(''累了'',没有,最后一个圣徒日。 / intent /是

可能是前者,但让计算机理解意图是

a非平凡的任务;)


只是几个清晨的想法...


-tkc



> >> template =''我是%s,他%s最后%s。''


>>> values =('''编码'','编码'',''周'')
格式=模板%值
格式化为



''我正在编码,他上周编码。''


>>> deformat(格式化,模板)



(''coding'',''coded'',' '周'')


扩展(为了更好的视觉效果):


>>> deformat(''我正在编码,他上周编码。'',''我是%s,他%s最后%s。'')



('''编码'',''编码'',''周'')


它会返回一个元组字符串,因为它无法告诉每个项目的原始类型是什么




任何输入?我查看了字符串模块

和re模块的文档,搜索了文档并搜索了这个

组,然后空出来 - 手。



是的,在您提供的简单案例中,可以使用re模块轻松完成



>> import re
template =''我是%s,他%s最后%s。''
values =('''编码'',''编码'',''周'')
格式=模板%值
unformat_re = re.escape(模板).replace(''%s'',''(。*)'')
#unformat_re = unformat_re.replace(''%i'',''([0-9] +)'' )
r = re.compile(unformat_re)
r.match(格式化).groups()



(''编码'',''编码'',''周'')


当你有像


>> answer =''将值格式化为strin g''
template =''格式化字符串%% s用于%s''%answer





>> template =''该值为%0 *。* f ''
values =(10,4,3.14159)
formatted = template%values
格式化



''值为00003.1415''




>> template =''亲爱的%(姓名)s,谢谢你的%(礼物)。它



非常%(adj)。''%{''name'':'''奶奶'',''礼物'':''毛衣'',''adj'':

''很好''}


此外,当事情变得有点纠结替换

值重复模板中的事项。如果没有格式化,那么我累了,而且他不会持续所有的圣日。将

解析为(''累'',没有上次,所有圣日)或

(''累了'',没有,最后一个圣徒日。 / intent /是

可能是前者,但让计算机理解意图是

a非平凡的任务;)


只是一些清晨的想法...


-tkc



Is there any builtin function or module with a function similar to my
made-up, not-written deformat function as follows? I can''t imagine it
would be too easy to write, but possible...

>>template = ''I am %s, and he %s last %s.''
values = (''coding'', "coded'', ''week'')
formatted = template % values
formatted

''I am coding, and he coded last week.''

>>deformat(formatted, template)

(''coding'', ''coded'', ''week'')

expanded (for better visual):

>>deformat(''I am coding, and he coded last week.'', ''I am %s, and he %s last %s.'')

(''coding'', ''coded'', ''week'')

It would return a tuple of strings, since it has no way of telling what
the original type of each item was.
Any input? I''ve looked through the documentation of the string module
and re module, did a search of the documentation and a search of this
group, and come up empty-handed.

解决方案

Dustan wrote:

Is there any builtin function or module with a function similar to my
made-up, not-written deformat function as follows? I can''t imagine it
would be too easy to write, but possible...

>>>template = ''I am %s, and he %s last %s.''
values = (''coding'', "coded'', ''week'')
formatted = template % values
formatted

''I am coding, and he coded last week.''

>>>deformat(formatted, template)

(''coding'', ''coded'', ''week'')

expanded (for better visual):

>>>deformat(''I am coding, and he coded last week.'', ''I am %s, and he %s
last %s.'')

(''coding'', ''coded'', ''week'')

It would return a tuple of strings, since it has no way of telling what
the original type of each item was.
Any input? I''ve looked through the documentation of the string module
and re module, did a search of the documentation and a search of this
group, and come up empty-handed.

Simple, but unreliable:

>>import re
template = "I am %s, and he %s last %s."
values = ("coding", "coded", "week")
formatted = template % values
def deformat(formatted, template):

.... r = re.compile("(.*)".join(template.split("%s")))
.... return r.match(formatted).groups()
....

>>deformat(formatted, template)

(''coding'', ''coded'', ''week'')

Peter


>>>template = ''I am %s, and he %s last %s.''

>>>values = (''coding'', "coded'', ''week'')
formatted = template % values
formatted

''I am coding, and he coded last week.''

>>>deformat(formatted, template)

(''coding'', ''coded'', ''week'')

expanded (for better visual):

>>>deformat(''I am coding, and he coded last week.'', ''I am %s, and he %s last %s.'')

(''coding'', ''coded'', ''week'')

It would return a tuple of strings, since it has no way of telling what
the original type of each item was.

Any input? I''ve looked through the documentation of the string module
and re module, did a search of the documentation and a search of this
group, and come up empty-handed.


Yes, in the trivial case you provide, it can be done fairly
easily using the re module:

>>import re
template = ''I am %s, and he %s last %s.''
values = (''coding'', ''coded'', ''week'')
formatted = template % values
unformat_re = re.escape(template).replace(''%s'', ''(.*)'')
# unformat_re = unformat_re.replace(''%i'', ''([0-9]+)'')
r = re.compile(unformat_re)
r.match(formatted).groups()

(''coding'', ''coded'', ''week'')

Thing''s get crazier when you have things like

>>answer =''format values into a string''
template = ''The formatting string %%s is used to %s'' % answer

or

>>template = ''The value is %0*.*f''
values = (10, 4, 3.14159)
formatted = template % values
formated

''The value is 00003.1415''

or

>>template = ''Dear %(name)s, Thank you for the %(gift)s. It

was very %(adj).'' % {''name'': ''Grandma'', ''gift'': ''sweater'', ''adj'':
''nice''}

Additionally, things go a little tangled when the replacement
values duplicate matters in the template. Should the unformatting
of "I am tired, and he didn''t last last All Saint''s Day" be
parsed as (''tired'', "didn''t last", "All Saint''s Day") or
(''tired'', "didn''t", "last All Saint''s Day"). The /intent/ is
likely the former, but getting a computer to understand intent is
a non-trivial task ;)

Just a few early-morning thoughts...

-tkc



>>>template = ''I am %s, and he %s last %s.''

>>>values = (''coding'', "coded'', ''week'')
formatted = template % values
formatted

''I am coding, and he coded last week.''

>>>deformat(formatted, template)

(''coding'', ''coded'', ''week'')

expanded (for better visual):

>>>deformat(''I am coding, and he coded last week.'', ''I am %s, and he %s last %s.'')

(''coding'', ''coded'', ''week'')

It would return a tuple of strings, since it has no way of telling what
the original type of each item was.

Any input? I''ve looked through the documentation of the string module
and re module, did a search of the documentation and a search of this
group, and come up empty-handed.


Yes, in the trivial case you provide, it can be done fairly
easily using the re module:

>>import re
template = ''I am %s, and he %s last %s.''
values = (''coding'', ''coded'', ''week'')
formatted = template % values
unformat_re = re.escape(template).replace(''%s'', ''(.*)'')
# unformat_re = unformat_re.replace(''%i'', ''([0-9]+)'')
r = re.compile(unformat_re)
r.match(formatted).groups()

(''coding'', ''coded'', ''week'')

Thing''s get crazier when you have things like

>>answer =''format values into a string''
template = ''The formatting string %%s is used to %s'' % answer

or

>>template = ''The value is %0*.*f''
values = (10, 4, 3.14159)
formatted = template % values
formated

''The value is 00003.1415''

or

>>template = ''Dear %(name)s, Thank you for the %(gift)s. It

was very %(adj).'' % {''name'': ''Grandma'', ''gift'': ''sweater'', ''adj'':
''nice''}

Additionally, things go a little tangled when the replacement
values duplicate matters in the template. Should the unformatting
of "I am tired, and he didn''t last last All Saint''s Day" be
parsed as (''tired'', "didn''t last", "All Saint''s Day") or
(''tired'', "didn''t", "last All Saint''s Day"). The /intent/ is
likely the former, but getting a computer to understand intent is
a non-trivial task ;)

Just a few early-morning thoughts...

-tkc



这篇关于反向字符串格式化(可能?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆