unicode string format misterious KeyError. What's wrong?

Barney Szabolcs

I have the following code at the unicode representation of a django models.Model:

def __unicode__(self):
    if self.right:
        return u"{left} ({left_score}) | {right} ({right_score})".format({
            'left': self.left, 
            'left_score': self.left_score, 
            'right': self.right, 
            'right_score': self.right_score,
        })
    else:
        return "%s" % self.left

I get

Exception Type: KeyError
Exception Value: u'left'

I also tried using unicode keys in the dictionary. self.left is not None.
I have read lots of forums still can't figure out what I am doing wrong. :(

How can I fix this?

Tim Wakeham

The format method requires you to pass your arguments as kwargs, not as a dictionary.

def __unicode__(self):
    if self.right:
        return u"{left} ({left_score}) | {right} ({right_score})".format(
            left=self.left, 
            left_score=self.left_score, 
            right=self.right, 
            right_score=self.right_score,
        )
    else:
        return "%s" % self.left

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

What's wrong with this format string?

From Dev

What's wrong with this sscanf() format string?

From Dev

What's wrong with this Haskell unicode variable name?

From Dev

PHP string - what's wrong with '<'?

From Dev

JSON string with format() keyerror

From Dev

python string format keyError

From Dev

String Format wrong format for %

From Dev

What's wrong with this string_to_number function?

From Dev

What's wrong with this string equality comparison? (Python)

From Dev

what's wrong with string contains match

From Dev

What is this misterious entry on my FritzBox router

From Dev

How to format with a UNICODE string to JINJA's variable in a template?

From Dev

What is wrong with my string format? (Even WITH the trailing comma in the parameter)

From Dev

what is the meaning of colon in python's string format?

From Dev

What's the significance of this string format in URLs?

From Dev

What is a unicode string?

From Dev

KeyError when using .format on a string in Python

From Dev

Python's .format() minilanguage and Unicode

From Dev

what's wrong with my function (removing string from text file)

From Dev

What's wrong with my Regex string for scraping link elements

From Dev

Powershell killing processes - what's wrong with my string?

From Dev

What is wrong with my sscanf format

From Dev

What is wrong with this JSON string?

From Dev

What is wrong with this JSON string?

From Dev

Writing unicode with python - what is wrong with this character

From Dev

Writing unicode with python - what is wrong with this character

From Dev

What's wrong with PhoneNumberFormattingTextWatcher?

From Dev

What's wrong with Gulp?

From Dev

What's wrong with strcmp?

Related Related

HotTag

Archive