1

我正在尝试用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)
defenderbar = 100 - attackerbar

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

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

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

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

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

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

result = 'Div.1 :: %s [ %s p ] [ %s% ] [ %s Dom ] <--> [ %s Dom ] [ %s% ] [ %s p ] %s ::' % (attacker, attackerpoint, attackerbar, attackerdom, defenderdom, defenderbar, defenderpoint, defender)
return result

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

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

我尝试用 () 更改 [ ] 并且错误是ValueError: unsupported format character ')' (0x5d)

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

4

2 回答 2

3

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

于 2012-09-25T15:48:52.307 回答
3

那里有两个%字符不是要格式化的字符。您需要将其加倍以使 Python 忽略它:

result = 'Div.1 :: %s [ %s p ] [ %s%% ] [ %s Dom ] <--> [ %s Dom ] [ %s%% ] [ %s p ] %s ::' % (attacker, attackerpoint, attackerbar, attackerdom, defenderdom, defenderbar, defenderpoint, defender)
于 2012-09-25T15:49:08.670 回答