字符串格式与映射& '*'......这是一个错误吗? [英] string formatting with mapping & '*'... is this a bug?
问题描述
嗨!
Python基本参考 - 第47页上的第2版,声明字符串
格式可以包括*。对于场宽(没有注意到限制);然而......
"%* d" %(6,2)#按预期工作
''2''
现在,有了映射....
mapping = {''one'':1,''two'':2}
这些按预期工作......
"%(two) d" %mapping
''2''"%(two)6d" %mapping
''2''
尝试通过''*'指定字段宽度到映射失败....
%(2)* d %(映射,6)
回溯(最近一次调用最后一次):
文件"< stdin>",第1行,在?
TypeError :format需要映射%(two) - * d %(6,映射)
回溯(最近一次调用最后一次):
文件"< stdin>" ;,第1行,在?
TypeError:格式需要映射
我期待的太多了......?
Pierre
Pierre Fortin写道:
试图指定一个通过''*''到映射的字段宽度失败....
"%(two)* d" %(mapping,6)回溯(最近一次调用最后一次):
文件"< stdin>",第1行,在?
TypeError:format需要映射%(two) - * d" %(6,映射)Traceback(最近一次调用最后一次):
文件"< stdin>",第1行,在?
TypeError:format需要映射
我不认为这与*事物有任何关系。你不能将
混合为%的序列和映射形式。例如,这也失败了
m = {''one'':1,''two'':2}
%(two)d%d ; %(m,6)
回溯(最近一次调用最后一次):
文件"< ; pyshell#8>",第1行,in -ople /
"%(two)d%d" %(m,6)
TypeError:格式需要映射
错误信息告诉你实话:你正在通过%
运算符一个元组,但你的格式字符串有一个%(两个)在它里面,
意味着它不需要元组而是映射。
-
--OKB(不是okblacke )
Brendan Barnwell
不要追随路径可能导致的地方。转而去,那里有
没有路径,并留下痕迹。
- author unknown
< blockquote> 2004年9月9日18:45:37 GMT OKB写道:
Pierre Fortin写道:
试图通过指定字段宽度'*''映射失败....
> "%(2)* d" %(mapping,6)
Traceback(最近一次调用最后一次):
文件"< stdin>",第1行,在?
TypeError:format需要一个映射> "%(2) - * d" %(6,映射)
回溯(最近一次调用最后一次):
文件"< stdin>",第1行,在?
TypeError:format需要一个映射
我认为这与*事物无关。你不能混合%的序列和映射形式。例如,这也失败了
m = {''one'':1,''two'':2}
" ;%(二)d%d %(m,6)
回溯(最近一次呼叫最后):
文件"< pyshell#8>",第1行,in -ople /
"%(两个)d%d %(m,6)
TypeError:格式需要映射
错误信息告诉你实话:你正在通过%
运算符一个元组,但是你的格式字符串有一个%(两个)在它中,这意味着它不需要元组而是映射。
Darn ...我希望Python能让我用<格式化字符串br />
以比这样更可读/可维护的方式映射:
mapping = {''one'':1,''two'':2,' 'size'':6}
eval('''%%(二)%(大小)dd''%%映射"%mapping)
''2''
-or- size = 6
eval("''%%(%)%dd''%% mapping"%size)
>
''2''
将我在P.48上的书副本改为:
"""
此外,在任何**宽度字段中,星号(*)字符可用于代替任何数字
。如果存在,宽度将从元组中的下一个
项目中读取。
**除了上面(1)中描述的映射
""
Pierre
> Darn ...我希望Python能让我使用映射来格式化字符串,其方式比以下更可读/可维护:
也许你可以简单地组合序列和地图 - 如果你没有
数字键,那可能会有用。像这样:
vals = [''first'',''second'']
map = {''foo'':''bar ''}
map2 = dict(map.items()+ [(str(i),v)for i,v in enumerate(vals)])
print"%(0)s%(foo)s%(1)s" %map2
不完美,但恕我直言比你的基于eval的解决方案更好。
-
问候,
Diez B. Roggisch
Hi!
"Python Essential Reference" - 2nd Ed, on P. 47 states that a string
format can include "*" for a field width (no restrictions noted); yet...
"%*d" % (6,2) # works as expected '' 2''
Now, with a mapping....
mapping = {''one'':1,''two'':2}
These work as expected...
"%(two)d" % mapping ''2'' "%(two)6d" % mapping '' 2''
Attempting to specify a field width via ''*'' to a mapping fails....
"%(two)*d" % (mapping,6) Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: format requires a mapping "%(two)-*d" % (6,mapping)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: format requires a mapping
Am I expecting too much...?
Pierre
Pierre Fortin wrote:
Attempting to specify a field width via ''*'' to a mapping fails...."%(two)*d" % (mapping,6) Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: format requires a mapping "%(two)-*d" % (6,mapping) Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: format requires a mapping
I don''t think this has anything to do with the * thing. You can''t
mix the sequence and mapping forms of %. For instance, this also fails
m = {''one'': 1, ''two'': 2}
"%(two)d %d" % (m, 6)
Traceback (most recent call last):
File "<pyshell#8>", line 1, in -toplevel-
"%(two)d %d" % (m, 6)
TypeError: format requires a mapping
The error message is telling you the truth: you''re passing the %
operator a tuple, but your format string has a "%(two)" in it, which
means it requires not a tuple but a mapping.
--
--OKB (not okblacke)
Brendan Barnwell
"Do not follow where the path may lead. Go, instead, where there is
no path, and leave a trail."
--author unknown
On 9 Sep 2004 18:45:37 GMT OKB wrote:
Pierre Fortin wrote:Attempting to specify a field width via ''*'' to a mapping fails....> "%(two)*d" % (mapping,6)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: format requires a mapping> "%(two)-*d" % (6,mapping)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: format requires a mapping
I don''t think this has anything to do with the * thing. You can''t
mix the sequence and mapping forms of %. For instance, this also failsm = {''one'': 1, ''two'': 2}
"%(two)d %d" % (m, 6)
Traceback (most recent call last):
File "<pyshell#8>", line 1, in -toplevel-
"%(two)d %d" % (m, 6)
TypeError: format requires a mapping
The error message is telling you the truth: you''re passing the %
operator a tuple, but your format string has a "%(two)" in it, which
means it requires not a tuple but a mapping.
Darn... I was hoping that Python would let me format strings with
mappings in a more readable/maintainable way than thusly:
mapping = {''one'':1,''two'':2,''size'':6}
eval("''%%(two)%(size)dd'' %% mapping" % mapping) '' 2''
-or- size=6
eval("''%%(two)%dd'' %% mapping" % size)
'' 2''
Changing my copy of the book on P.48 to read:
"""
In addition, the asterisk (*) character may be used in place of any number
in any** width field. If present, the width will be read from the next
item in the tuple.
** EXCEPT with mappings described in (1) above
"""
Pierre
> Darn... I was hoping that Python would let me format strings withmappings in a more readable/maintainable way than thusly:
Maybe you can simply combine the sequence and the map - if you don''t have
numeric keys, that might work. Like this:
vals = [''first'' , ''second'']
map = {''foo'' : ''bar''}
map2 = dict(map.items() + [(str(i), v) for i, v in enumerate(vals)])
print "%(0)s %(foo)s %(1)s" % map2
Not perfect, but IMHO better than your eval-based solution.
--
Regards,
Diez B. Roggisch
这篇关于字符串格式与映射& '*'......这是一个错误吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!