ValueError: unsupported format character ']' (0x5d)

Question:

I’m trying to make irc bot with python. The bot will parse XML and paste its content on channel. This is part of my code

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

and I got ValueError: unsupported format character ']' (0x5d).

I’m pretty sure I already close all [ ]

I tried change [ ] with () and the error is ValueError: unsupported format character ')' (0x5d)

Can anyone show me where I made a boo boo ?
Thank you

Asked By: user1695285

||

Answers:

You’ve twice put [ %s% ], which includes an extra % after the s. Python interprets that as %], which is invalid.

Answered By: Daniel Roseman

You have two % characters in there that are not meant to be formatting characters. You need to double it to make Python ignore it:

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

QUESTION:
Same Error at this line but I can’t seem to find any %

WHERE (@IncludeLeavers = 'Yes' OR @IncludeLeavers= 'No' AND Staff.IsCurrent = 1)
""",

        params={
          "IncludeLeavers": REPORT_OPTIONS["IncludeLeavers"][0]
        })

Error on this line IncludeLeavers": REPORT_OPTIONS["IncludeLeavers"][0]

Answered By: Zedverse07
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.