ValueError:不支持的格式字符']'(0x5d)

时间:2012-09-25 15:46:50

标签: python bots irc

我正在尝试用python制作irc bot。机器人将解析XML并将其内容粘贴到频道上。这是我的代码的一部分

f = open("1.xml")
data = f.read()
f.close()
domi = parseString(data)
attackerbartag = domi.getElementsByTagName('bar')[0].toxml()
attackerbar = attackerbartag.replace('<bar>','').replace('</bar>','')
attackerbar = round(float(attackerbar)2)
deffenderbar = 100 - attackerbar

attackertag = domi.getElementsByTagName('name')[1].toxml()
attacker = attackertag.replace('<name>','').replace('</name>','')

deffendertag = domi.getElementsByTagName('name')[42].toxml()
deffender = deffendertag.replace('<name>','').replace('</name>','')

attackerpointtag = domi.getElementsByTagName('points')[1].toxml()
attackerpoint = attackerpointtag.replace('<points>','').replace('</points>','')

deffenderpointtag = domi.getElementsByTagName('points')[6].toxml()
deffenderpoint = deffenderpointtag.replace('<points>','').replace('</points>','')

attackerdomtag = domi.getElementsByTagName('domination')[0].toxml()
attackerdom = attackerdomtag.replace('<domination>','').replace('</domination>','')

deffenderdomtag = domi.getElementsByTagName('domination')[4].toxml()
deffenderdom = deffenderdomtag.replace('<domination>','').replace('</domination>','')

result = 'Div.1 :: %s [ %s p ] [ %s% ] [ %s Dom ] <--> [ %s Dom ] [ %s% ] [ %s p ] %s ::' % (attacker, attackerpoint, attackerbar, attackerdom, deffenderdom, deffenderbar, deffenderpoint, deffender)
return result

我得到了ValueError: unsupported format character ']' (0x5d).

我很确定我已经关闭了所有[]

我尝试使用()更改[],错误为ValueError: unsupported format character ')' (0x5d)

有谁能告诉我我在哪里制作嘘声? 谢谢

2 个答案:

答案 0 :(得分:3)

您已经两次[ %s% ],其中包含s之后的额外%。 Python将其解释为%],这是无效的。

答案 1 :(得分:3)

你有两个%个字符,并不是要格式化字符。你需要加倍才能让Python忽略它:

result = 'Div.1 :: %s [ %s p ] [ %s%% ] [ %s Dom ] <--> [ %s Dom ] [ %s%% ] [ %s p ] %s ::' % (attacker, attackerpoint, attackerbar, attackerdom, deffenderdom, deffenderbar, deffenderpoint, deffender)