在 Python 中将字符串转换或取消格式化为变量(如 format(),但相反) [英] Convert, or unformat, a string to variables (like format(), but in reverse) in Python

查看:25
本文介绍了在 Python 中将字符串转换或取消格式化为变量(如 format(),但相反)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 Version 1.4.0\nVersion 1.15.6\n 形式的字符串,我想要一种提取这三个数字的简单方法从他们.我知道我可以使用 format 方法将变量放入字符串中;我基本上想倒着做,就像这样:

# 所以我知道我可以这样做:x, y, z = 1, 4, 0打印 '版本 {0}.{1}.{2}\n'.format(x,y,z)# 输出是'版本 1.4.0\n'# 但我希望能够逆转它:mystr='版本 1.15.6\n'a, b, c = mystr.unformat('版本 {0}.{1}.{2}\n')# 结果是 a, b, c = 1, 15, 6

我发现有人问了同样的问题,但回复是针对他们的特定情况的:反向使用Python格式字符串进行解析

一个通用的答案(如何反向执行 format() )会很棒!不过,针对我的具体案例的答案也会非常有帮助.

解决方案

实际上 Python 正则表达式库已经提供了您需要的一般功能.你只需要稍微改变模式的语法

<预><代码>>>>进口重新>>>from 操作符导入 itemgetter>>>mystr='版本 1.15.6\n'>>>m = re.match('Version (?P<_0>.+)\.(?P<_1>.+)\.(?P<_2>.+)', mystr)>>>地图(itemgetter(1),排序(m.groupdict().items()))['1', '15', '6']

如您所见,您必须将 (un) 格式字符串从 {0} 更改为 (?P<_0>.+).您甚至可以使用 (?P<_0>\d+) 要求小数点.此外,您必须对某些字符进行转义以防止它们被解释为正则表达式特殊字符.但这反过来可以再次自动化,例如与

<预><代码>>>>re.sub(r'\\{(\d+)\\}', r'(?P<_\1>.+)', re.escape('版本 {0}.{1}.{2}'))'版本\\(?P<_0>.+)\\.(?P<_1>.+)\\.(?P<_2>.+)'

I have strings of the form Version 1.4.0\n and Version 1.15.6\n, and I'd like a simple way of extracting the three numbers from them. I know I can put variables into a string with the format method; I basically want to do that backwards, like this:

# So I know I can do this:
x, y, z = 1, 4, 0
print 'Version {0}.{1}.{2}\n'.format(x,y,z)
# Output is 'Version 1.4.0\n'

# But I'd like to be able to reverse it:

mystr='Version 1.15.6\n'
a, b, c = mystr.unformat('Version {0}.{1}.{2}\n')

# And have the result that a, b, c = 1, 15, 6

Someone else I found asked the same question, but the reply was specific to their particular case: Use Python format string in reverse for parsing

A general answer (how to do format() in reverse) would be great! An answer for my specific case would be very helpful too though.

解决方案

Actually the Python regular expression library already provides the general functionality you are asking for. You just have to change the syntax of the pattern slightly

>>> import re
>>> from operator import itemgetter
>>> mystr='Version 1.15.6\n'
>>> m = re.match('Version (?P<_0>.+)\.(?P<_1>.+)\.(?P<_2>.+)', mystr)
>>> map(itemgetter(1), sorted(m.groupdict().items()))
['1', '15', '6']

As you can see, you have to change the (un)format strings from {0} to (?P<_0>.+). You could even require a decimal with (?P<_0>\d+). In addition, you have to escape some of the characters to prevent them from beeing interpreted as regex special characters. But this in turm can be automated again e.g. with

>>> re.sub(r'\\{(\d+)\\}', r'(?P<_\1>.+)', re.escape('Version {0}.{1}.{2}'))
'Version\\ (?P<_0>.+)\\.(?P<_1>.+)\\.(?P<_2>.+)'

这篇关于在 Python 中将字符串转换或取消格式化为变量(如 format(),但相反)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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