views:

82

answers:

1

The following code raise a keyError exception:

addr_list_formatted = []
addr_list_idx = 0
    for addr in addr_list: # addr_list is a list
        addr_list_idx = addr_list_idx + 1
        addr_list_formatted.append("""
            "{0}"
            {
            "gamedir"  "str"
            "address"  "{1}"
            }
        """.format(addr_list_idx, addr))

Why? Using python 3.1. Thanks.

+4  A: 

The problem are those { and } characters you have there that doesn't specify a key for the formatting. You need to double them up, so change the code to:

addr_list_formatted.append("""
    "{0}"
    {{
    "gamedir"  "str"
    "address"  "{1}"
    }}
""".format(addr_list_idx, addr))
Lasse V. Karlsen
YOU ARE CORRECT!!! THANK YOU!!! :) I was so desperate because of this problem... thanks!!!
Dor