خرید بک لینک

Vote count: 0

I have a dictionary like so:

{
    'cost' : cost,
    'oLen' : oLen
}

I write this to file so the file contains:

{'oLen': 32, 'cost': 2048}

Later, I do this:

    with open('conf.conf') as f:
        config = json.loads(f.read())
    print config['oLen']

And get this:

ValueError: Expecting property name: line 1 column 2 (char 1)

If I change json.loads to json.dumps, I get:

TypeError: string indices must be integers, not str

asked 4 mins ago

3 Answers

Vote count: 1

You need to use double quotes in your JSON.

If you use json.dump to write the original dictionary to the file, you won't need to worry about it though!

answered 1 min ago

Vote count: 0

Change your single quotes to double quotes:

{"oLen": 32, "cost": 2048}

JSON is picky about that.

answered 1 min ago

Vote count: -1

You need to use Double Quotations

{"oLen": 32, "cost": 2048}

JSON example

answered 6 secs ago

برچسب: نویسنده: استخدام کار تاريخ: جمعه 28 اسفند 1394 ساعت: 3:14

صفحه بندی